#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define pb emplace
#define pbb emplace_back
const int mod = 1e9 + 7;
ll qpow(ll x, ll n) {
	ll an = 1;
	x %= mod;
	while (n) {
		if (n & 1) (an *= x) %= mod;
		(x *= x) %= mod, n >>= 1;
	}
	return an;
}
int T;
ll n, m, v, a[100050];
ll ans, t;
map<int, ll> cd;
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr), cout.tie(nullptr);
	for (cin >> T; T--;) {
		cin >> n >> m >> v;
		cd.clear();
		ans = -1;
		for (int i = 1, c, d; i <= m; ++i) {
			cin >> a[i] >> d;
			c = a[i];
			if (cd[c] && ans) {
				ans = 1;
				if (cd[c] != d) ans = 0;
			}
			cd[c] = d;
		}
		if (!ans) {
			cout << "0\n";
			continue;
		}
		if (ans == -1 && m == n) {
			cout << qpow(1 + (v - 1) * v, n - 1) << '\n';
			continue;
		} if (m == 1) {
			cout << qpow(v * v, n - 1) << '\n';
			continue;
		}
		sort(a + 1, a + 1 + m);
		m = unique(a + 1, a + 1 + m) - a - 1;
		t = 0;
		for (int i = 1; i < m; ++i)
			if (a[i] + 1 == a[i + 1]) ++t;
		cout << (qpow(v * v, n - 1 - t) * qpow(1 + (v - 1) * v, t) % mod) % mod << '\n';
	}
	return 0;
}
