#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<map>
using namespace std;

const int N = 1e5+10, mod = 1e9+7;
#define ll long long

int T; 
int n, m, v;

struct node{
	int id, val;
} a[N];

bool cmp(node x, node y) {
	return x.id==y.id ? x.val<y.val : x.id<y.id;
}

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

int c[20], d[20];
int A[20], B[20];
ll ans;

void dfs(int x) {
	if(x == n) {
		for(int i = 1; i <= n; i++) d[i] = c[i];
		int flag = 0;
		for(int i = 1; i < n; i++) {
			if(A[i] == c[i]) {
				if(c[i+1] == 0) c[i+1] = B[i];
				else if(B[i] != c[i+1]) flag = 1;
			}
		}
		for(int i = 1; i <= n; i++) c[i] = d[i];
		if(!flag) ans++;
		return;
	}
	A[x] = 1, B[x] = 1;
	dfs(x+1); 
	A[x] = 2, B[x] = 1;
	dfs(x+1); 
	A[x] = 1, B[x] = 2;
	dfs(x+1); 
	A[x] = 2, B[x] = 2;
	dfs(x+1); 
}

void bf() {
	map<int,int> mp; bool flag = 0;
	for(int i = 1; i <= n; i++) c[i] = 0;
	for(int i = 1, x, y; i <= m; i++) {
		scanf("%d%d",&x,&y), c[x] = y;
		if(mp.find(x)!=mp.end()) {
			if(mp[x] != y) flag = 1;
		}
		mp[x] = y;
	}
	if(flag) {
		printf("0\n");
		return;
	}
	ans = 0;
	dfs(1);
	printf("%lld\n",ans%mod);
}

void solve() {
	scanf("%d%d%d",&n,&m,&v);
	
	if(n <= 12 && m <= 12 && v <= 2) {
		bf();
		return;
	}
	
	map<int,int> mp; bool flag = 0; int tot = 0;
	for(int i = 1, x, y; i <= m; i++) {
		scanf("%d%d",&x,&y);
		if(mp.find(x)!=mp.end()) {
			if(mp[x] != y) flag = 1;
		}
		else {
			tot++;
			a[tot].id = x, a[tot].val = y, mp[x] = y;
		}
	}
	
	if(flag) {
		printf("0\n");
		return;
	}
	
	m = tot;
	sort(a+1,a+m+1,cmp);
	
	if(m <= 1) {
		printf("%lld\n",qpow(v,2*(n-1)));
		return;
	}
	
	if(m == n) {
		printf("%lld\n",qpow(1ll*v*v-v+1,n-1));
		return;
	}
	
	ll ans = 1;
	ans = ans*qpow(v,2*(a[1].id-1))%mod;
	for(int i = 2; i <= m; i++) {
		ll temp = qpow(v,2*(a[i].id-a[i-1].id+1))-qpow(v,a[i].id-a[i-1].id-1);
		temp = (temp%mod+mod)%mod;
		ans = ans*temp%mod;
	}
	ans = ans*qpow(v,2*(n-a[m].id))%mod;
	printf("%lld\n",ans);
}

int main() {
	freopen("assign.in","r",stdin); 
	freopen("assign.out","w",stdout);

	scanf("%d",&T);
	for(int i = 1; i <= T; i++) {
//		printf("%d ",i);
		solve();
//		printf("\n");
	}

	return 0;
}
