#include<bits/stdc++.h>
#define up(i, a, b) for(int i = (int)a; i <= (int)b; ++i)
#define dn(i, a, b) for(int i = (int)a; i >= (int)b; --i)
#define int long long

using namespace std;

const int MN = 1e6 + 5;
const int Mod = 1e9 + 7;

int ksm(int a, int b){
	int res = 1;
	for(; b; b >>= 1, a = a * a % Mod) if(b & 1) res = res * a % Mod;
	return res;
}

int n, m, w;
int vis[MN], f[MN], g[MN];

int F(int i);

int G(int i){
	if(g[i]) return g[i];
	if(i == n) return 1;
	if(vis[i]){
		return g[i] = (((vis[i + 1]) ? 1 : w) * F(i + 1) + (w - 1) * w % Mod * G(i + 1) % Mod) % Mod;
	}
	else{
		return g[i] = w * w % Mod * G(i + 1) % Mod;
	}
}

int F(int i){
	if(f[i]) return f[i];
	if(i == n) return 1;
	return f[i] = (((vis[i + 1]) ? 1 : w) * F(i + 1) + (w - 1) * w % Mod * G(i + 1) % Mod) % Mod;
}

void solve(){
	memset(f, 0, sizeof(f));
	memset(g, 0, sizeof(g));
	memset(vis, 0, sizeof(vis));
	scanf("%lld%lld%lld", &n, &m, &w);
	bool cp = 0;
	if(m == 1 && n > 1000000) cp = 1;
//	printf("FK: %d\n", x);
	int flag = 1;
	up(i, 1, m){
		int x, d; scanf("%lld%lld", &x, &d);
		if(!cp && vis[x] && vis[x] != d){
			flag = false;
		}
		if(!cp) vis[x] = d;
	}
	if(cp){
		printf("%lld\n", ksm(w, 2 * n - 2));
		return;
	}
	if(!flag){
		puts("0");
		return;
	}
	printf("%lld\n", G(1));
}

signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T; cin >> T;
	while(T--){
		solve();
	}
	
	return 0;
}
