#include<bits/stdc++.h>
#define gt getchar()
#define pt putchar
#define ll long long
using namespace std;
ll getin(){
	ll ans=0,f=1;
	char c=gt;
	while(!isdigit(c)){
		if(c=='-') f=-1;
		c=gt;
	}
	while(isdigit(c)){
		ans=(ans<<1)+(ans<<3)+(c^48);
		c=gt;
	}
	return ans*f;
}
void puto(ll x){
	if(x<0){
		pt('-');
		x=-x;
	}
	if(x>=10) puto(x/10);
	pt(x%10+'0');
}
const ll mo=1e9+7,N=1e5+5;
ll ksm(ll a,ll b){
	ll ans=1,base=a;
	while(b){
		if(b&1) ans=(ans*base)%mo;
		b>>=1;
		base=(base*base)%mo;
	}
	return ans%mo;
}
ll a[N];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ll T=getin();
	while(T--){
		memset(a,0,sizeof(a));
		ll n,m,v,sum=0;
		bool f=0,f1=1;
		n=getin(),m=getin(),v=getin();
		for(ll i=1;i<=m;i++){
			ll c=getin(),d=getin();
			if(m==1) break;
			if(!a[c]) sum++;
			if(c!=i) f1=0;
			if(a[c]!=0&&a[c]!=d) f=1;
			a[c]=d;
		}
		if(f) printf("0");
		else if(m==1||sum==1) puto(ksm((v*v)%mo,n-1));
		else if(f1){
			ll ans=0,k=(v*(v-1)+1)%mo;
			ans=ksm(k,m-1);
			if(n==m) puto(ans);
			else puto((ans+ksm((v*v)%mo,n-m)%mo)%mo);
		}
		else {
			ll k=(v*(v-1)+1)%mo,d=(v*v)%mo,ans=1;
			for(int i=2;i<=n;i++){
				if(a[i]&&a[i-1]) ans=(ans*k)%mo;
				else ans=(ans*d)%mo;
			}
			puto(ans);
		}
		pt('\n');
	}




	return 0;
}


