#include<bits/stdc++.h>
using namespace std;

#define rep(i,a,n) for(int i=a;i<=n;++i)
#define N 100005
#define mod 1000000007
#define ll long long

int T,n,m,m0;
ll v;
struct node{
	int a,b;
	inline bool operator<(node &bbb){
		return a<bbb.a;
	}
}nds[N];

inline ll qpow(ll x,int k){ //x<=mod---?
//	cout<<"qpow(): "<<x<<" "<<k<<"\n";
	ll ret=1;
	while(k){
		if(k&1)ret*=x,ret%=mod;
		x*=x; x%=mod;
		k>>=1;
	}
//	cout<<"qpow--\n";
	return ret;
}

int cur=1;
ll ny[N],p2=mod-2;
ll jc[N],njc[N];
inline void count(int m0){
	while(cur<m0){
		++cur;
		ny[cur]=qpow(cur,p2);
		jc[cur]=jc[cur-1]*cur; jc[cur]%=mod;
		njc[cur]=njc[cur-1]*ny[cur]; njc[cur]%=mod;
	}
	return;
}

inline ll cc(int k,int m0){
	ll ret=1;
	ret*=jc[m0]; ret%=mod;
	ret*=njc[m0-k]; ret%=mod;
	ret*=jc[k]; ret%=mod;
	return ret;
	
//	for(int i=m0;i>=m0-k+1;--i){
//		ret*=i;
////		cout<<"*"<<i<<"\n";
////		cout<<ret<<"\n";
//	}
//	rep(i,1,k){
//		ret/=i;
////		cout<<"/"<<i<<"\n";
////		cout<<ret<<"\n";
//	}
//	ret%=mod;
//	return ret;
}

ll tot,cnt,ans,tmp;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
//	ios::sync_with_stdio(false);
//	cin.tie(0);
	
	ny[1]=1; jc[1]=1; njc[1]=1;
	jc[0]=1; njc[0]=1;
	cin>>T;
	int a,b;
//	int t=0;
	while(T--){
//		++t;
//		cout<<"T:"<<t<<"---\n";
		cin>>n>>m>>v;
//		cout<<"nmv"<<" "<<n<<" "<<v<<"\n";
		rep(i,1,m){
			cin>>nds[i].a>>nds[i].b;
		}
		sort(nds+1,nds+1+m);
//		rep(i,1,m){
//			cout<<nds[i].a<<" "<<nds[i].b<<"\n";
//		}
		bool flag=false;
		m0=m;
		rep(i,2,m){
			if(nds[i].a==nds[i-1].a)--m0;
			if(nds[i].a==nds[i-1].a&&nds[i].b!=nds[i-1].b){
				cout<<0<<"\n"; //<<"³õÊ¼ÖµÃ¬¶Ü\n";
				flag=true;
				break;
			}
		}
		if(flag)continue;
//		if(n==m0){ //---------------AAAAA
//			cout<<1<<"ÌîÂú\n";
//			continue;
//		}
		tot=0; cnt=0;
		tot=qpow((v*v)%mod,n-1);
		if(m0==1){
//			cout<<n<<" "<<m<<" "<<v<<"\n";
			ans=tot;
			if(ans<0)ans+=mod;
			cout<<ans<<"\n"; //<<"ÈÎÒâ\n";
			continue;
		}
		count(m0);
		tmp=qpow(v,n-m0);
//		cout<<tmp;
		for(int k=1;k<=m0-1;++k){
//			cout<<cc(k-1,m0-1)<<" "<<qpow(v-1,m0-k)<<"\n";
			cnt+=(cc(k-1,m0-1)*qpow(v-1,m0-k))%mod;
			cnt%=mod;
		}
		cnt*=tmp; cnt%=mod;
//		cout<<tot<<" "<<cnt<<"tete\n";
		ans=tot-cnt;
		if(ans<0)ans+=mod;
		cout<<ans<<"\n";
	}

	return 0;
}


