#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e5+10;
const ll MOD = 1e9+7;
int n,m,v,c[N],d[N];
/*
void work1(){
	int 
	for(int a=0;a<(1<<(n-1)))
	for(int i=0;i<(1<<n);i++){
		bool flag=true;
		for(int j=1;j<=n;j++)if((i>>(c[j]-1))!=d[j]){
			flag=false;
			break;
		}
		if(!flag)continue;
		
	}
		for()
}
*/
ll Qpow(ll a,int x){
	ll r=1;
	for(;x;x>>=1,a=a*a%MOD)if(x&1)
		r=r*a%MOD;
	return r;
}
void work2(){
	if(d[1]<1||d[1]>v)printf("0\n");
	printf("%lld\n", Qpow(v,2*n-2));
}
void work3(){
	ll it=1ll*v*v-v+1;
	printf("%lld\n", Qpow(it,n-1));
}
void work(){
	scanf("%d%d%d", &n, &m, &v);
	bool flag3=n==m;
	for(int i=1;i<=m;i++){
		scanf("%d%d", c+i, d+i);
		if(c[i]!=i)flag3=false;
	}
	if(m==1)work2();
	else if(flag3)work3();
	else printf("0\n");
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%d", &T);
	while(T--)work();
	return 0;
}

