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

int t,n,m,v,c,d,vis[1000005],flag,dp[1000005][2],mod=1e9+7,tmp,tmp2;
signed main() {
	//freopen("assign3.in","r",stdin);
	//freopen("out.txt","w",stdout);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> t;
	while (t--) {
		cin >> n >> m >> v;
		tmp = ((v%mod)*(v%mod))%mod,tmp2 = (((v-1)%mod)*(v%mod))%mod;
		flag = 0;
		for (int i = 1;i <= n;i++) vis[i] = 0;
		for (int i = 1;i <= m;i++) {
			cin >> c >> d;
			if (vis[c] && vis[c] != d) flag = 1;
			else vis[c] = d;
		}
		if (flag) {
			cout << 0 << endl;
			continue;
		}
		for (int i = 0;i <= n;i++) dp[i][0] = dp[i][1] = 0;
		if (!vis[1]) dp[1][0] = 1;
		else dp[1][1] = 1;
		for (int i = 2;i <= n;i++) {
			if (vis[i]) {
				dp[i][0] = 0;
				dp[i][1] = (dp[i-1][0]*tmp)%mod+(dp[i-1][1]*(tmp2+1))%mod;
				dp[i][1] %= mod;
			} else {
				dp[i][0] = (dp[i-1][0]*tmp)%mod+(dp[i-1][1]*tmp2)%mod;
				dp[i][1] = dp[i-1][1]*v;
				dp[i][0] %= mod,dp[i][1] %= mod;
			}
		}
		cout << (dp[n][0] + dp[n][1])%mod << endl;
	}
	
	return 0;
}
