#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+5;	
const int mod=1e9+7; 	
int T,n,m,c[N],d[N],p[N];
ll v,f[N],g[N];
map<int,int> ap;
ll qmi(ll n,ll m){
	ll res=1;
	while(m){
		if(m&1) res=res*n%mod;
		n=n*n%mod;
		m>>=1; 
	}
	return res;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%lld",&n,&m,&v);
		if(n>100000){
			printf("0\n");
			break;
		}
		ap.clear();
		bool no=false;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&c[i],&d[i]);
			p[i]=c[i];
			if(ap.count(c[i])&&ap[c[i]]!=d[i]) no=true;
			ap[c[i]]=d[i];
		}
		if(no){
			printf("0\n");
			continue;
		}
		if(ap.count(1)&&ap.count(2)) f[2]=v%mod,g[2]=(v-1)*(v-1)%mod;
		else if(ap.count(1)) f[2]=v%mod,g[2]=(v-1)*v%mod;
		else if(ap.count(2)) f[2]=v%mod,g[2]=(v-1)*v%mod;
		else f[2]=0,g[2]=v*v%mod;
		for(int i=3;i<=n;i++){
			if(ap.count(i)){
				f[i]=(f[i-1]*v%mod+g[i-1]*v*v%mod)%mod;
				g[i]=(f[i-1]*((v-1)*(v-1)%mod)%mod+g[i-1]*v*mod*(v-1)%mod)%mod;
			}	
			else{
				f[i]=(f[i-1]*v)%mod;
				g[i]=(f[i-1]*(v-1)%mod*v%mod+g[i-1]*(v-ap.count(i-1))%mod*v%mod)%mod;
			}		
		}
		printf("%lld\n",(f[n]+g[n])%mod);
	}
 	return 0;
}
