#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll T;
ll n,m,v;
ll a[100010];
ll c[100010];
ll d[100010];
ll f[100010];
ll pf[1000010];
struct T2{
	ll c,d;
} p[1000010];
bool cmp(T2 x,T2 y){
	return x.c<y.c;
}
ll qm(ll x,ll y){
	ll ret=1;
	while(y){
		if(y%2) ret=ret*x%mod;
		x=x*x%mod;y/=2;
	}
	return ret;
}
void sol(){
	scanf("%lld%lld%lld",&n,&m,&v);
	
	for(int i=1;i<=m;i++){
		scanf("%lld%lld",&p[i].c,&p[i].d);
	}
	sort(p+1,p+m+1,cmp);
	for(int i=1;i<m;i++){
		if(p[i].c==p[i+1].c&&p[i].d!=p[i+1].d){
			printf("0\n"); return ;
		}
	}
	if(n==m){
		ll pri=qm((v-1)*v%mod+1,n-1);
		printf("%lld\n",pri);
		return ;
	}
	if(m<=1) {
		printf("%lld\n",qm(v,2*n-2));
		return ;
	}
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		sol() ;
	}
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
