/*life is a dream*/
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+5;
const int mod=1e9+7;
ll T,n,v,m;
map<int ,int> a;
bool flag,allnum;
ll ksm(ll base,ll k){
	ll ans=1;
	while(k){
		if(k&1){
			ans=(ans*base)%mod;
		}
		base=(base*base)%mod;
		k=k>>1; 
	}
	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	//cout<<ksm(2,1000000000);
	cin>>T;
	while(T--){
		flag=0,allnum=1;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			int c,d;
			cin>>c>>d;
			if(a[c]==0 || a[c]==d){
				a[c]=d;
			}
			else{
				flag=1;
			}
		}
		if(flag){
			cout<<"0\n";
			flag=0;
			a.clear();
			continue;
		}
		//for(int i=1;i<=n;i++)	cout<<a[i]<<" ";
		for(int i=1;i<=n;i++){
			if(a[i]==0) allnum=0;
		}
		if(m==1){
			cout<<ksm(v,2*(n-1));
		}
		else if(m==n && allnum){
			cout<<ksm((v*(v-1)+1)%mod,n-1);
		}
		else{
			ll ans=1;
			for(int i=1;i<n;i++){
				if(a[i]!=0 && a[i+1]!=0){
					ans*=(v-1)*v+1;
				}else{
					ans*=v*v;
				}
				ans=ans%mod;
			}
			cout<<ans;
		}
		
		a.clear();
		cout<<"\n";
	}
	return 0;
}


