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

void fre(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	return;
}

const int mod = 1e9+7;
int n, m, v;
int rir[100086];

int read(){
	int res = 0;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' && c <= '9'){
		res *= 10;
		res += c - '0';
		c = getchar();
	}
	return res;
}

struct P{
	int c, d;
	bool operator <(const P &tmp) const{
		return c <= tmp.c;
	}
} p[100086];

ll ksm(ll a, ll b){
	ll res = 1;
	while(b){
		if(b & 1) (res *= a) %= mod;
		(a *= a) %= mod;
		b >>= 1;
	}
	return res % mod;
}

void love(){
	scanf("%d%d%d", &n, &m, &v);
	int c, d;
	for(int i = 1; i <= m; i++){
//		scanf("%d%d", &c, &d);
		c = read();
		d = read(); 
		p[i].c = c;
		p[i].d = d;
	}
	if(v == 1){
		puts("1");
		return;
	}
	sort(p+1, p+1+m);
//	for(int i = 1; i <= m; i++){
//		printf("*%d %d\n", p[i].c, p[i].d);
//	}
	int cnt = 0;
	for(int i = 1; i <= m; i++){
//					printf("^%d %d\n", p[i].c, p[i].d);
		if(p[i].d == 1e9+7) continue;
		rir[++cnt] = p[i].c;
		for(int j = i + 1; j <= m; j++){
			if(p[i].c == p[j].c){
				if(p[i].d != p[j].d){
					puts("0");
					return;
				}else{
					p[j].d = 1e9+7;
				}
			}
		}
	}
	
//					for(int i = 1; i <= cnt; i++){
//						printf("*%d ", rir[i]);
//					}
//					puts("");
	
	ll v2 = 1ll * v * v % mod;
	ll ans = ksm(v2, rir[1] - 1);
	ll nir = (v2 - v + 1) % mod;
	(nir += mod) %= mod;
	nir %= mod;
	int nircnt = 0;
	if(m == 1){
		printf("%d\n", ksm(v2, n-1));
		return;
	}
	for(int i = 1; i < cnt; i++){
		if(rir[i+1] - rir[i] == 1){
			nircnt++;
		}else{
			(ans *= ((ksm(v2, rir[i+1] - rir[i]) - (v - 1) * ksm(v, rir[i+1] - rir[i] - 1) % mod + mod) % mod)) %= mod;
		}
	}
	(ans *= ksm(nir, nircnt)) %= mod;
	(ans *= ksm(v2, n - rir[cnt])) %= mod;
	printf("%d\n", ans);
	return;
}

int main(){
	fre();
	int T;
	scanf("%d", &T);
	while(T--) love();
	return 0;
}
