#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
long long T,n,m,v,a[10000001],b[100000001];
long long c[10000001],d[10000001];
long long ans;
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v);
		for(long long i=1;i<=m;i++){
			scanf("%lld%lld",&c[i],&d[i]);
		}
		if(m==1){
			ans=1;
			for(long long i=1;i<n;i++){
				ans*=v;
				ans%=mod;
				ans*=v;
				ans%=mod;
			}
			printf("%lld\n",ans);
			continue;
		}
		printf("0\n");
	}
	return 0;
}
