#include <bits/stdc++.h>
#define mod 1000000007
//#define int long long
using namespace std;
const int sizen = 100003;
pair<int, int> p[sizen], b[sizen];
int dp[sizen][2];
int qpow(int a, int b) {
	int ans = 1;
//	cout << b << endl;
	while (b > 0) {
		if (b & 1) ans = 1ll * ans * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return ans;
}
signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0);
	int T; cin >> T; while (T--) {
		int n, m, v;
		cin >> n >> m >> v;
		int M = 0;
		for (int i = 1; i <= m; ++i) cin >> b[i].first >> b[i].second;
		sort(b + 1, b + m + 1);
		bool Flag = false;
		for (int i = 1; i <= m; ++i) {
			if (b[i].first != b[i - 1].first) p[++M] = b[i];
			else if (b[i].second != p[M].second) Flag = true; 
		}
		if (Flag) {
			cout << "0" << endl;
			continue;
		}
		m = M;
//		dp[0] = 1;
		bool flag = false;
		if (p[m].first == n) --m, flag = true;
		for (int i = 1; i <= m; ++i) dp[i][0] = dp[i][1] = 0;
		if (n == m) {
			cout << 0 << endl;
			continue;
		}
		dp[1][0] = v - 1;
		dp[1][1] = 1;
		for (int i = 2; i <= m; ++i) {
			if (p[i].first != p[i - 1].first + 1) {
				dp[i][0] = 1ll * (1ll * dp[i - 1][0] * qpow(v, 2 * (p[i].first - p[i - 1].first - 1)) % mod * v % mod * (v - 1) % mod + 1ll * dp[i - 1][1] * (1ll * qpow(v, 2 * (p[i].first - p[i - 1].first - 1)) * v % mod * (v - 1) % mod - qpow(v, (p[i].first - p[i - 1].first - 1)) * v % mod + mod)) % mod;
				dp[i][1] = 1ll * (dp[i - 1][0] + dp[i - 1][1]) * v % mod * qpow(v, 2 * (p[i].first - p[i - 1].first - 1)) % mod;
			} else {
				dp[i][0] = 1ll * (1ll * dp[i - 1][0] * v % mod + 1ll * dp[i - 1][1] % mod) * (v - 1) % mod;
				dp[i][1] = 1ll * (1ll * dp[i - 1][0] * v % mod + 1ll * dp[i - 1][1] % mod); 
			}
//			cout << dp[i][0] << " " << dp[i][1] << endl;
		}
		int ans = 0;
		if (flag && p[m].first == n - 1) {
			ans = ans + 1ll * (1ll * dp[m][0] * v % mod + dp[m][1]) % mod;
		} else if (flag) {
//			cout << 1ll * dp[m][1] * v % mod * (1ll * qpow(v, 2 * (n - 1 - p[m].first)) - 1ll * qpow(v, (n - 1 - p[m].first - 1)) * (v - 1) % mod + mod) % mod << " " <<  1ll * qpow(v, (n - 1 - p[m].first - 1)) * (v - 1) << " " << dp[m][1] * v % mod * qpow(v, 2 * (n - 1 - p[m].first)) << endl;;
			ans = ans + (1ll * dp[m][0] * v % mod * qpow(v, 2 * (n - 1 - p[m].first)) % mod + 1ll * dp[m][1] * v % mod * (1ll * qpow(v, 2 * (n - 1 - p[m].first)) - 1ll * qpow(v, (n - 1 - p[m].first - 1)) * (v - 1) % mod + mod) % mod) % mod;
		} else ans = ans + 1ll * (dp[m][0] + dp[m][1]) * v % mod * qpow(v, 2 * (n - 1 - p[m].first)) % mod;
//		cout << dp[m][0] + dp[m][1] << "@" << (dp[m][0] + dp[m][1]) * v % mod << endl;
//		cout << "------------" << endl;
//		cout << ( n  - 1 - m ) * 2 << endl;
//		ans = 1ll * ans * qpow(v, ((n - 1) - m) * 2) % mod;
//cout << ans << endl;
		cout << 1ll * ans * qpow(v, 2 * (p[1].first - 1)) % mod << endl;
	}
	return 0;
}
