#include<bits/stdc++.h>
using namespace std;
const int N=1000006,mod=1000000007;
long long t,a[N];
long long flaga=-1,flagb=-1;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--){
		long long n,m,v,f=0,sum=0;
		scanf("%d%d%d",&n,&m,&v);
		memset(a,0,sizeof(int)*(n+2));
		for(int i=1;i<=m;i++){
			long long c,d;
			scanf("%d%d",&c,&d);
			if(c!=i||n!=m){
				flaga=0;
			}
			if(d!=1){
				flagb=0;
			}
			if(a[c]&&a[c]!=d){
				sum=0;
				cout<<sum<<endl;
				f=1;
				break;
			}else{
				a[c]=d;
			}
		}
		if(f==1) continue;
		if(flaga==-1){
			cout<<((n*(n-1)%mod)*(n-1))%mod;
		}else{
			cout<<"0"<<endl;
		}
	}
	return 0;
}
