#include<bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
int T,x,y;
int n,m;
long long v;
map<int,int> rc;
void dfs(int x)
{
	
}
long long ksm(long long d,long long c)
{
	long long ans=1;
	while(c)
	{
		if(c&1)
		{
			ans=(ans*d)%mod;
		}
		c>>=1;
		d=(d*d)%mod;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--)
	{
		rc.clear();
		scanf("%d %d %lld",&n,&m,&v);
		bool cft=false;
		int cnt=m;
		for(int i=1;i<=m;i++)
		{
			scanf("%d %d",&x,&y);
			if(rc.find(x)!=rc.end())
			{
				if(rc[x]!=y)
				{
					cft=true;
				}else{
					cnt--;
				}
			}
			rc[x]=y;
		}
		if(cft)
		{
			printf("0\n");
			continue;
		}
		if(m==1||cnt==1)
		{
			long long ds=v*v%mod,cs=(n-1)%mod;
			long long ans=ksm(ds,cs);
			printf("%lld\n",ans);
			continue;
		}
		if(v<=2)
		{
			dfs(1);
			cout<<"0\n";
			continue;
		}
		cout<<"0\n";
	}
	return 0;
}
/*
O(n)=80pts

20pts current.

129600=4*4*4*9*9*25=2^6*3^4*5^2
*/
