#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7,N=1e6+10;
int t,n,m,v,x,y,vis[N],q,ans;
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--){
		ans=1;
		q=0;
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++){
			cin>>x>>y;
			if(vis[x]&&vis[x]!=y) q=1;
			vis[x]=y;
		}
		if(q){
			for(int i=1;i<=max(v,n);i++){
				vis[i]=0;
			}
			cout<<"0\n";
			continue;
		}
		for(int i=1;i<n;i++){
			if(vis[i]&&vis[i+1]){
				ans=(ans*(v*(v-1)+1))%mod;
			}
			else{
				ans=(ans*v*v)%mod;
			}
		}
		cout<<ans%mod<<"\n";
		for(int i=1;i<=max(v,n);i++){
				vis[i]=0;
		}
	}
	return 0;
}
