#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5 + 20, Mod = 1e9+7;
ll T, n, m, v, f[N], cnt = 0;
struct node{
	ll p, d;
}a[N], b[N];
bool cmp(node a, node b){return a.p < b.p;}
map<ll, bool> vis;
ll quick_pow(ll a, ll x){
	ll res = 1;
	while(x){
		if(x&1) res = (res*a)%Mod;
		a = a*a%Mod;
		x = x>>1;
	}
	return res;
}
int main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);

	scanf("%lld", &T);
	while(T--) {
//		vis.clear();
		cnt = 0;
		bool flag =  0;
		scanf("%lld%lld%lld", &n, &m, &v);
		for(int i = 1; i <= m; i++) {
			scanf("%lld%lld", &a[i].p, &a[i].d);
		}
		sort(a+1, a+m+1, cmp);
		for(int i = 1; i <= m; i++) {
			if(a[i].p == a[i+1].p && a[i].d != a[i+1].d) {
				puts("0");
				flag = 1;
				break;
			}
			if(a[i].p != a[i+1].p) b[++cnt] = a[i];
		}
		if(flag == 1) continue;
		b[0].p = 2;
		if(b[cnt].p != n) b[++cnt].p = n, b[cnt].d = -1;
		f[0] = 1;
		for(int i = 1; i <= cnt; i++) {
			if(b[i].p == 1) {
				f[i] = 1;
				continue;
			}
			if(b[i].d == -1) f[i] = f[i-1] * quick_pow(v, (b[i].p-b[i-1].p)*2)%Mod;
			else if(b[i].p-b[i-1].p <= 1 || b[i].p == 2) f[i] = f[i-1] * (v*(v-1)%Mod+1)%Mod;
			else if(b[i].p-b[i-1].p >  1) f[i] = f[i-1] * quick_pow(v, (b[i].p-b[i-1].p-1)*2+1)%Mod;
		}
		printf("%lld\n", f[cnt]);
	}
	return 0;
} 
/*
1
2 2 2
1 1
1 2
*/
