#include <bits/stdc++.h>
#define int long long
using namespace std;

const int mod = 1e9 + 7;

int T, n, m, v;

map<int, int> mp;

int fpow(int x, int k) {
	int ans = 1;
	while (k) {
		if (k & 1) ans = (ans % mod * (x % mod)) % mod;
		x = x % mod * (x % mod) % mod;
		k >>= 1;
	}
	return ans % mod;
}

void cleanup() {
	mp.clear();
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> T;
	while (T--) {
		cleanup();
		cin >> n >> m >> v;
		bool flag = true;
		bool flag1 = true;
		for (int i = 1;i <= m;++i) {
			int c, d;
			cin >> c >> d;
			if (c != i) flag1 = false;
			if (mp.count(c) != 0) {
				if (mp[c] != d) flag = false;
			}
			mp[c] = d;
		}
		if (flag1 && m == n) {
			cout << fpow(v*v - v + 1, n-1) % mod << endl;
			continue;
		} else if (!flag) {
			cout << 0 << endl;
			continue;
		}
		
		int ans = fpow(v, 2*(n-1)) % mod;
		int ivsq = fpow(v % mod * v % mod, mod-2) % mod;
		
		// cout << "dbg: " << ans << endl;
		
		int lst = -1;
		
		for (auto u : mp) {
			if (u.first == lst+1) {
				ans = ans % mod * ivsq % mod;
				ans = ans * (v*v % mod -v+1) % mod;
			}
			lst = u.first;
		}
		
		cout << ans << endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}