#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
	ll c,d;
}a[100005];
bool cmp(node x,node y){
	return x.c<y.c;
}
ll mod=1e9+7;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ll t;
	cin>>t;
	while(t--){
		map<ll,ll>mp;
		ll n,m,v,ans=0;
		cin>>n>>m>>v;
		for(ll i=1;i<=m;i++){
			cin>>a[i].c>>a[i].d;
			if(mp[a[i].c]&&mp[a[i].c]!=a[i].d){
				ans=-1;
			}
			mp[a[i].c]=a[i].d;
		}
		if(ans==-1){
			cout<<"0\n";
			continue;
		}
		ans=1;
		for(ll i=1;i<n;i++){
			int h=v,res;
			if(mp[i]){
				h=1;
			}
			if(mp[i+1]){
				res=v*(v-1)+h;
			}else{
				res=v*(v-1)+v;
			}
			ans=(ans*res)%mod;
		}
		cout<<ans<<"\n";
	}
	return 0;
}
