#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e6+100,mod=1e9+7;
int n,m,v,now; int flag[N];
int dp[N][3],c[N/10],d[N/10];
int bb[N/10];
map<int,int>vis;
void work1(){
	bool yes=1;
	for(int i=0;i<=n+10;i++) dp[i][0]=dp[i][1]=0,flag[i]=0;
	for(int i=1;i<=m;i++){
		scanf("%lld%lld",&c[i],&d[i]);
		if(flag[c[i]] and flag[c[i]]!=d[i]) yes=0;
		else flag[c[i]]=d[i];
	}
	if(!yes){
		puts("0");
		return;
	}
	if(flag[n]) dp[n][1]=1,dp[n][0]=0;
	else dp[n][1]=dp[n][0]=1;
	for(int i=n-1;i>=1;i--){
		if(flag[i+1]) dp[i][1]=((v-1)*v%mod+1)*dp[i+1][1]%mod;
		else dp[i][1]=((v-1)*v%mod*dp[i+1][0]%mod+v*dp[i+1][1]%mod)%mod;
		if(!flag[i]){
			if(flag[i+1]) dp[i][0]=v*v%mod*dp[i+1][1]%mod;
			else dp[i][0]=v*v%mod*dp[i+1][0]%mod;
		}
	}
	if(flag[1]) cout<<dp[1][1]%mod<<endl;
	else cout<<dp[1][0]%mod<<endl;
	return;
}
struct frac{
	int a[4][4];
	frac(){memset(a,0,sizeof(a));}
	void build(){for(int i=1;i<=2;i++)a[i][i]=1;}
}ba;
frac mul(frac A,frac B){
	frac C;
	for(int i=1;i<=2;i++)
		for(int k=1;k<=2;k++)
			for(int j=1;j<=2;j++)
				C.a[i][j]=(C.a[i][j]+A.a[i][k]*B.a[k][j]%mod)%mod;
	return C;
}	
frac qpow(int ti){
	frac res; res.build();
	frac base=ba;
	while(ti){
		if(ti&1) res=mul(res,base);
		ti>>=1,base=mul(base,base);
	}
	return res;
}
void work2(){
	ba.a[1][1]=v*v%mod,ba.a[2][1]=0;
	ba.a[1][2]=v*(v-1)%mod,ba.a[2][2]=v;
	bool yes=1;
	for(int i=1;i<=m;i++){
		scanf("%lld%lld",&c[i],&d[i]);
		if(vis[c[i]]!=0 and vis[c[i]]!=d[i]) yes=0;
		else vis[c[i]]=d[i]; 
	}
	if(!yes){
		puts("0");
		return;
	}
	sort(c+1,c+1+m);
	int now=0,cnt=0;
	bb[++cnt]=0;
	for(int i=1;i<=m;i++){
		if(c[i]!=now) now=c[i],bb[++cnt]=c[i];
		else continue;
	}
	if(bb[cnt]!=n) bb[++cnt]=n;
	int dp0,dp1;
	if(vis[n]) dp0=0,dp1=1;
	else dp0=dp1=1;
	
	for(int i=cnt-1;i>=1;i--){	
		int bdp0=dp0,bdp1=dp1;
		if(bb[i+1]==bb[i]+1){
			if(i==cnt-1){
				if(!vis[n]) dp1=v*(v-1)*bdp0%mod+v*bdp1%mod,dp1%=mod,dp0=0;
				else dp1=(v*(v-1)%mod+1)*bdp1%mod,dp0=0;
			}
			else dp1=(v*(v-1)%mod+1)*bdp1%mod,dp0=0;
			if(bb[i]==1){
				cout<<dp1<<endl;
				return;
			}
			continue;
		}
		if(i==cnt-1){
			if(vis[n]) dp0=v*v%mod*bdp1%mod,dp1=((v-1)*v%mod+1)*bdp1%mod;
			else dp0=v*v%mod*bdp0%mod,dp1=v*(v-1)*bdp0+v*bdp1;
		}
		else dp0=v*v%mod*bdp1%mod,dp1=((v-1)*v%mod+1)*bdp1%mod;
		frac res=qpow(bb[i+1]-bb[i]-1-1);
		bdp0=dp0,bdp1=dp1;
		dp0=bdp0*res.a[1][1]%mod+bdp1*res.a[2][1]%mod,dp0%=mod;
		dp1=bdp0*res.a[1][2]%mod+bdp1*res.a[2][2]%mod,dp1%=mod;
		if(bb[i]==1){
			cout<<dp1<<endl;
			return;
		}
		else if(bb[i]==0){
			cout<<dp0<<endl;
			return;
		} 
	}
	return;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T; cin>>T;
	while(T--){
		now++;
		cin>>n>>m>>v;
		if(n<=N-100) work1();
		else{
			work2();
			for(int i=1;i<=m;i++) vis[c[i]]=0;
		}
	}
	return 0;
}
/*
1
7 1 2
4 1
*/
