#include <bits/stdc++.h>
using namespace std;
const int M=1e8+1;
const int mod=1e9+7;
int T,n,m,x,y;
long long v;
int a[M];
int o;
long long ans;
int num;
void solve(){
	for(int i=1;i<n;i++){
		if(a[i]&&a[i+1]) ans=(ans*((v*(v-1))+1))%mod; 
		else ans=(ans*((v*v)%mod))%mod;
	}
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		ans=1,num=0;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			scanf("%d%d",&x,&y);
			if(a[x]){
				if(a[x]!=y){
					cout<<0<<endl;
					o=1;
				}
				else num++;
			}
			a[x]=y;
		}
		m-=num;
		if(o) continue;
		if(n==m){
			for(int i=1;i<n;i++){
				ans=(ans*((v*(v-1))%mod+1))%mod; 
			}
			cout<<ans<<endl;
			continue;
		}
		else if(m==1){
			for(int i=1;i<n;i++){
				ans=(ans*((v*v)%mod))%mod;
			}
			cout<<ans<<endl;
			continue;
		}
		else if(n<=12){
			solve();
			cout<<ans<<endl;
			continue;
		}
	}
	
}
