#include<iostream>
#define int long long
using namespace std;
const int mod=1e9+7;
int ksm(int a,int k)
{
	int ans=1;
	while(k)
	{
		if(k&1) ans=(ans*a)%mod;
		a=(a*a)%mod;
		k/=2;
	}
	return ans;
}
int t;
int n,m,v,c[100001],d[100001];
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	for(int ll=1;ll<=t;ll++)
	{
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++) cin>>c[i]>>d[i];
		if(m==1) cout<<ksm(v,2*(n-1))<<endl;
	}
	return 0;
}
