#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5,M=1e9+7;
int T,n,m,siz;
ll v,ans;
struct A{
	int p,x;
}a[N];
bool cmp(A x,A y){
	return x.p<y.p;
}
ll qpow(ll a,ll b){
	ll ans=1;
	while(b){
		if(b&1)ans=ans*a%M;
		a=a*a%M;
		b>>=1;
	}
	return ans;
}
void solve(){
	scanf("%d%d%lld",&n,&m,&v);
	for(int i=1;i<=m;i++){
		scanf("%d%d",&a[i].p,&a[i].x);
	}
	sort(a+1,a+1+m,cmp);
	for(int i=2;i<=m;i++){
		if(a[i-1].p==a[i].p&&a[i-1].x!=a[i].x){
			printf("0\n");return ;
		}
	}
	siz=0;
	for(int i=1;i<=m;i++){
		if(i==1||a[i-1].p!=a[i].p){
			a[++siz]=a[i];
		}
	}
	ans=1;
	if(a[1].p>1){
		ans=ans*qpow(v,(a[1].p-1)*2)%M;
	}
	if(a[m].p<n){
		ans=ans*qpow(v,(n-a[siz].p)*2)%M;
	}
	for(int i=1;i<siz;i++){
		ll tmp1=qpow(v,(a[i+1].p-a[i].p)*2);
		ll tmp2=qpow(v,a[i+1].p-a[i].p-1)*(v-1)%M;
		ans=ans*(tmp1-tmp2+M)%M;
	}
	printf("%lld\n",ans);
}
int main(){//100pts
//	freopen("in.txt","r",stdin);
//	freopen("out2.txt","w",stdout);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	solve();
//	return 0;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;//10:10 ¹ý´óÑùÀý 
}/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/ 
