#include<bits/stdc++.h>
#define coin const int
#define ull unsigned long long
#define ll long long
#define int ll

using namespace std;
const int N = 1000000 + 10;
const int inf = 1000000007;

int read () {
	int x = 0, k = 1; char c = 0;
	while (c < '0' || c > '9') { if (c == '-') k = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
	return x * k;
}

int ksm (int x, int power, int mod = inf, int ret = 1) { for (; power; power >>= 1, x = x * x % mod) if (power & 1) ret = ret * x % mod; return ret; }
int inv (int x, int mod = inf) { return ksm(x, mod - 2, mod); }

int T;
int n, m, v, ret;
unordered_map<int, int> a;
int c[N], d[N];

void Solve1 () {
	ret = ksm(v + 1, n - 1);
	cout << ret << endl;
}

void Solve2 () {
	static int _1[N], _2[N], __[N];
	int All1 = (1 << (2 * n - 2)) - 1, All2 = (1 << n) - 1;
	for (int S1 = 0, f = 0; S1 <= All1; ++S1, ret += f, f = 0) {
		for (int i = 0; i < 2 * n - 2; i += 2) _1[i >> 1] = (S1 >> i) & 1, _2[i >> 1] = (S1 >> (i + 1)) & 1;
		for (int S2 = 0; S2 <= All2; ++S2) {
			for (int i = 0; i < n; ++i) __[i] = a[i + 1] ? (a[i + 1] - 1) : ((S2 >> i) & 1);
			for (int i = 1; i < n; ++i) if (__[i - 1] == _1[i - 1] && __[i] != _2[i - 1]) goto BREAK;
			f = 1; BREAK: continue;
		}
	}
	cout << ret << endl;
}

void Solve3 () {
	if (n < m) return cout << 0 << endl, void();
	ret = ksm(v, 2 * (n - m)) * ksm(v * v - 1, m - 1) % inf;
	cout << ret << endl;
}

void work () {
	n = read(), m = read(), v = read(), ret = 0, a.clear();
	for (int i = 1; i <= m; ++i) c[i] = d[i] = 0;
	for (int i = 1; i <= m; ++i) c[i] = read(), d[i] = read();

	for (int i = 1; i <= m; ++i) 
		if (!a[c[i]] || a[c[i]] == d[i]) a[c[i]] = d[i]; 
		else return puts("0"), void();

	if (n <= 6 && m <= 6 && v <= 2) Solve2();
	else if (n <= 12 && m <= 12 && v <= 2) Solve3();
	else if (n == m) Solve1();
	else cout << 0 << endl;
}

signed main () {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	T = read();
	while (T--) work();
	return 0;
}

/*
64 = 2^(3 * 2)
48 = 2^(2 * 2) * 3^(1)
36 = 2^(1 * 2) * 3^(2)

10
4 1 2
1 1
4 2 2
1 1
2 1
4 3 2
1 1
2 1
3 1
4 4 2
1 1
2 1
3 1
4 1

*/

