#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,v,t,ans,de,hg,liang;
ll a[1000005],b,c,d,mod=1e9+7;
bool cc[1000055],dd[1000005];
struct man{
	ll c;
	ll d;
}sui[1000005];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>t;
	while(t--){
		ans=1;
		liang=0;
		hg=1;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			cin>>sui[i].c>>sui[i].d;
			if(a[sui[i].c]==0)
			a[sui[i].c]=sui[i].d;
			else{
				hg=0;
			}
		}
		if(m==n){
			cout<<1<<endl;
			continue;
		}
		if(hg==0){
			cout<<0<<endl;
			continue;
		}
		if(m==1){
			ans=1;
			for(int i=1;i<=n-1;i++){
				ans*=v*v;
				ans%=mod;
			}
			cout<<ans<<endl;
			continue;
		}
		for(int op=1;op<=n-1;op++){
			if(a[op]==0){
				ans*=v;
				ans%=mod;
			}
			if(a[op+1]==0){
				ans*=v;
				ans%=mod;
			}	
		}
		cout<<ans<<endl;	
	}
	return 0;
}

