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