#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5, mod = 1e9 + 7;
int n, t, m, v, c[N], d[N];
int x[10000005];
long long qpow(long long a, long long b) {
	long long num = 1, base = a % mod;
	for (int i = b; i; i >>= 1, base = base * base % mod) {
		if (i & 1) num = num * base % mod;
	}
	return num % mod;
}
int main() {
	
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &t);
	while(t--) {
		memset(x, 0x80, sizeof(x));
		bool fA = 1, f = 0;
		int cnt = 0;
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &c[i], &d[i]);
			if (c[i] != i) fA = 0;
			if (x[c[i]] == d[i]) cnt++;
			else if (x[c[i]] > 0) {
				printf("0\n");
				f = 1;
			}
			x[c[i]] = d[i];
		}
		if (f) continue;
		if (v == 2) {
			
		}
		if (m == 1) {
			long long ans = v * v % mod;
			ans = qpow(ans, n - 1);
			printf("%lld\n", ans);
		}
		if (n == m && fA) {
			long long ans = 1;
			for (int i = 1; i <= n - 1; i++) {
				ans *= (1LL * v * (v - 1) % mod + 1) % mod;
				ans %= mod;
			}
			printf("%lld\n", ans);
		}
	}
	
	return 0;
}
