#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll read(){
	ll t1=0,t2=1;
	char c=getchar();
	while(c<'0'||c>'9'){
		if(c=='-')t2=-1;
		c=getchar();
	}while(c>='0'&&c<='9')t1=t1*10+c-'0',c=getchar();
	return t1*t2;
} 
const ll MOD=1000000007,N=1000001;
ll n,m,v;
ll d[N],a[N],b[N],c[N],asd[N],tong[N];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ll T;T=read();
	while(T--){
		memset(tong,0,sizeof tong);
		n=read();m=read();v=read();
		bool flag1=1,flag2=1,flag3=(v<=2)?1:0;
		if(m!=n)flag1=0;
		for(ll i=1;i<=m;i++){
			c[i]=read();d[i]=read();
			if(c[i]!=i)flag1=0;
			if(d[i]!=1)flag2=0;
			asd[c[i]]=d[i];
		}
		if(flag1){
			ll ans=0,temp=0;
			for(ll i=1;i<=n;i++){
				if(tong[d[i]]==0){
					temp++;
					tong[d[i]]=1;
				}
			}
			ans=1;
			for(ll i=1;i<=n-1;i++){
				ans=(ans*temp)%MOD;
			}
			if(v-temp!=0){
				for(ll i=1;i<=n-1;i++){
					ans=(((ans*(v-temp))%MOD)*v)%MOD;
				}	
			}
			cout<<ans%MOD<<'\n';
		}
	}
	return 0;
}
