#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
ll read(){
	ll x=0;int f=1;char c=getchar();
	while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();}
	while(c<='9'&&c>='0'){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
} 
const ll mod=1e9+7;
ll ans=1;
int T,lis[100005],nl[100005];
ll n,m,v;
ll qpow(ll a,ll b){
	ll kans=1;
	while(b>0){
		if(b&1)kans=(kans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return kans;
}
void wk(){
	int nm=0;
	ans=1;
	map<int,int>t;
	//cout<<n<<' '<<m<<' '<<v<<endl;
	for(int i=1,c,d,v;i<=m;i++){
		c=read();d=read();
		v=t[c];
		if(v&&v!=d){
			cout<<0<<endl;
			return ;
		}else {
			t[c]=d;
			lis[i]=c;
		}
	}
	sort(lis+1,lis+1+m);
	for(int i=1;i<=m;i++){
		if(nl[nm]==lis[i])continue;
		nl[++nm]=lis[i];
	}
	ll V=(v*v)%mod,VV=(v*(v-1))%mod;
	if(nm>=2){
		if(nl[2]-nl[1]>1)ans=qpow(V,lis[1]);
		else ans=(qpow(V,lis[1]-1)*(VV+1))%mod;
		for(int i=2;i<=nm;i++){
			ll k=nl[i]-nl[i-1]-1;
			if(k>0)ans=ans*(VV*qpow(V,k-1)%mod)%mod;
			if(nl[i]==n)continue;
			if(nl[i+1]-nl[i]>1)
				ans=(ans*V)%mod;
			else ans=ans*(VV+1)%mod;
		}
		ll k=n-nl[nm];
		ans=ans*qpow(V,k-1)%mod;
	}else if(nm==1){
		if(nl[1]==n)ans=qpow(V,n-1);
		else{
			ans=qpow(V,nl[1]);
			ll k=n-nl[1];
			ans=ans*qpow(V,k-1)%mod;
		}
	}
	cout<<ans<<endl;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--){
		n=read();m=read();v=read();
		wk();
	}
	return 0;
}




