#include<bits/stdc++.h>
using namespace std;
const long long N=1e6+5,mod=1e9+7;
long long t,m,n,v,data[N],f[N];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		memset(f,0,sizeof(f));
		f[0]=1;
		scanf("%lld%lld%lld",&n,&m,&v);
		bool isend=0;
		for(int i=1;i<=n;i++)data[i]=0;
		for(int i=1;i<=m;i++){
			long long c,d;
			scanf("%lld%lld",&c,&d);
			if(data[c]!=0&&data[c]!=d){
				printf("0\n");
				isend=1;
			}
			data[c]=d;
		}
		if(isend)continue;
		for(int i=1;i<n;i++){
			if(data[i]&&data[i+1])f[i]=(f[i-1]*(v*(v-1)+1))%mod;
			else if(data[i-1]&&data[i]==0&&data[i+1])f[i]=f[i-1]/v*((v*v-1)*v+1)%mod;
			else f[i]=f[i-1]*v*v%mod;
		}
		printf("%lld\n",f[n-1]);
	}
	return 0;
}
