#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
int t,n,m,v,a[1000010],x[100010],y[100010],ans;
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--){
		ans=1;
		memset(a,0,sizeof(a));
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			cin>>x[i]>>y[i];
		}
		if(m==1){
			for(int i=1;i<n;i++){
				ans=(ans*(v*v%mod))%mod;
			}
			cout<<ans%mod<<endl;
			continue;
		}
		int flag=0;
		int m1=m;
		for(int i=1;i<=m;i++){
			if(a[x[i]]&&a[x[i]]!=y[i]){
				flag=1;
				cout<<0<<endl;
				break;
			}
			if(a[x[i]])m1--;
			a[x[i]]=y[i];
		}
		if(flag)continue;
		if(m1==n){
			for(int i=1;i<n;i++){
				ans=(ans*((((v-1)*v)%mod)+1))%mod;
			}
			cout<<ans%mod<<endl;
			continue;
		}
		cout<<0<<endl;
	}
}
