#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define umap unordered_map
#define empb emplace_back

const ll mod = 1e9 + 7, MAXN = 1e6 + 5;

ll n = 0, m, v, dp[2][4] = {{}};
umap<ll, ll> lim;

void init() {
	memset(dp, 0, sizeof dp);
	lim.clear();
}
int mian() {
	cin >> n >> m >> v;
//	cerr << n << ' ' << m << ' ' << v << endl;
	bool noans = 0;
	for (ll i = 1; i <= m; ++i) {
		ll c, d;
		cin >> c >> d;
		if (lim[c] && lim[c] != d) {
			noans = 1;
		}
		lim[c] = d;
	}
	if (noans) {
		cout << 0 << endl;
		return 0;
	}
	for (ll i = 1; i <= n; ++i) {
		if (lim[i]) {
			lim[i] = 1;
//			cerr << i << endl;
		}
	}
	ll sigma_last = 0;
	if (lim[1]) {
		dp[1][lim[1]] = 1;
		sigma_last = 1;
	} else {
		dp[1][0] = 1;
	}
	for (ll i = 2; i <= n; ++i) {
		memcpy(dp[0], dp[1], sizeof dp[0]);
		memset(dp[1], 0, sizeof dp[1]);
		ll k = lim[i - 1];
		// dp[i][0]
		if (lim[i]) {
			dp[1][0] = 0;
		} else {
			if (k) {
				dp[1][0] = dp[0][k] * v % mod * (v - 1) % mod;
			} else {
				dp[1][0] = (sigma_last * v % mod * (v - 1) % mod + v * v % mod * dp[0][0] % mod) % mod;
			}
		}
		// dp[i][1]
		if (lim[i]) {
			if (k) {
				dp[1][1] = dp[0][k] * (1 + v * (v - 1) % mod) % mod;
			} else {
				dp[1][1] = (sigma_last * (1 + v * (v - 1) % mod) % mod + dp[0][0] * v % mod * v % mod) % mod;
			}
		} else {
			if (k) {
				dp[1][1] = dp[0][k];
			} else {
				dp[1][1] = sigma_last;
			}
		}
		// dp[i][2]
		if (lim[i]) {
			dp[1][2] = 0;
		} else {
			if (k) {
				dp[1][2] = dp[0][k] * (v - 1) % mod;
			} else {
				dp[1][2] = sigma_last * (v - 1) % mod;
			}
		}
		sigma_last = 0;
		for (ll j = 1; j <= 2; ++j) {
			(sigma_last += dp[1][j]) %= mod;
		}
	}
	if (lim[n]) {
		cout << dp[1][lim[n]] << endl;
	} else {
		cout << (sigma_last + dp[1][0]) % mod << endl;
	}
	return 0;
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ll T;
	cin >> T;
	while (T--) {
//		cerr << "------------------------------------------------------" << endl;
		init();
		mian();
	}
	return 0;
}













































