#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<ll,ll> ma;
ll T,n,m,v;
ll ans;
ll c,d;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		ma.clear();
		ans=0;
		cin>>n>>m>>v;
		int bj=0;
		for(ll i=1;i<=m;i++){
			cin>>c>>d;
			if(ma.count(c)&&ma[c]!=d){
				cout<<0<<endl;
				bj=1;
				break;
			}
			ma[c]=d;
		}
		if(bj) continue;
		if(m==1){
			ans=1;
			for(ll i=1;i<n;i++){
				if(ma.count(i)&&ma.count(i+1)){
					ans=((ans*(v-1))%1000000007*v)%1000000007+ans;
				}else{
					ans*=v;
					ans=ans%1000000007;
					ans*=v;
				}
				ans=ans%1000000007;
			}
			cout<<ans<<endl;
			continue;
		}
		if(m==n){
			ans=1;
			for(ll i=1;i<n;i++){
				ans=((ans*(v-1))%1000000007*v)%1000000007+ans;
				ans=ans%1000000007;
			}
			cout<<ans<<endl;
			continue;
		}
		
		ans=1;
		for(ll i=1;i<n;i++){
			if(ma.count(i)&&ma.count(i+1)){
				ans=((ans*(v-1))%1000000007*v)%1000000007+ans;
			}else if(ma.count(i)){
				ma[i+1]=1;
				ans*=v;
				ans=ans%1000000007;
				ans*=v;
			}else{
				ans*=v;
				ans=ans%1000000007;
				ans*=v;
			}
			ans=ans%1000000007;
		}
		cout<<ans<<endl;
	}
	return 0;
} 
