#include<bits/stdc++.h>
using namespace std;
long long t,n,m,v,c,d,a,b,i,j;
vector<pair<long long,long long> >x;
long long ksm(long long a,long long b){
	long long r=1;
	while(b){
		if(b&1)r=r*a%1000000007;
		a=a*a%1000000007;
		b>>=1;
	}
	return r;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		if(!m){
			printf("%lld\n",ksm(v,2*n-2));
			goto g;
		}
		x.clear();
		while(m--){
			scanf("%lld%lld",&c,&d);
			x.push_back(make_pair(c,d));
		}
		sort(x.begin(),x.end());
		c=ksm(v,2*x[0].first-2);
		for(i=1;i<x.size();i++){
			if(x[i].first==x[i-1].first){
				if(x[i].second!=x[i-1].second){
					printf("0\n");
					goto g;
				}
				continue;
			}
			c=c*((ksm(v,2*x[i].first-2*x[i-1].first)-ksm(v,x[i].first-x[i-1].first-1)*(v-1)+1000000007)%1000000007)%1000000007;
		}
		c=c*ksm(v,2*n-2*x[x.size()-1].first)%1000000007;
		printf("%lld\n",(c+1000000007)%1000000007);
		g:;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
