#include<bits/stdc++.h>
using namespace std;
long long t,n,m,v;
int c[100005],d[100005];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	for(int o=1;o<=t;o++){
		cin>>n>>m>>v;
		int p=0;
		for(int i=1;i<=m;i++){
			cin>>c[i]>>d[i];
			if(d[i]>v){
				cout<<0<<endl;
				p=1;
			} 
		}
		if(p==1)continue;long long ans=1;
		if(n==m){
			
			for(int i=1;i<n;i++){
				ans=(ans*((v-1)*v+1))%1000000007;
			}			
		}
		else if(m==1){
			for(int i=1;i<n;i++){
				ans=(ans*v*v)%1000000007;
			}
			if(c[1]!=1)ans/=v;
		}
		cout<<ans<<endl;
	}
	return 0;
} 
