#include<bits/stdc++.h>
const int N=1e5+10,MOD=1e9+7;
std::pair<int,int> a[N];
int qpow(int a,int b){
	int ret=1;
	while(b){
		if(b&1) ret=1ll*ret*a%MOD;
		a=1ll*a*a%MOD;b>>=1;
	}
	return ret;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--){
		int n,m,v;scanf("%d%d%d",&n,&m,&v);
		for(int i=1;i<=m;i++) scanf("%d%d",&a[i].first,&a[i].second);
		std::sort(a+1,a+m+1);
		int ans=qpow(v,2*(a[1].first-1+n-a[m].first));
		for(int i=2;i<=m;i++){
			if(a[i].first==a[i-1].first){
				if(a[i].second!=a[i-1].second) {ans=0;break;}
				else continue;
			}
			ans=1ll*ans*(qpow(v,2*(a[i].first-a[i-1].first))+MOD-1ll*qpow(v,a[i].first-a[i-1].first-1)*(v-1)%MOD)%MOD;
		}
		printf("%d\n",ans);
	}
}
