#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll T,n,m,vv;
int x,y,v[100010];
ll work(ll a,ll b){
	ll base=a,ans=1;
	while(b)
	{
		ans=(ans*base)%mod;
		if(b&1)
			base=(base*base)%mod; 	
		b=b/2;
		
	}

	return ans;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--)
	{
		scanf("%lld%lld%lld",&n,&m,&vv);
		bool ok=1;
		for(int i=1;i<=m;i++)
		{
			cin>>x>>y;
			if(!v[x])
				v[x]=y;
			else if(v[x]!=y)
			{
				ok=0;
				continue;
			}
		}
		if(ok)
		printf("%lld\n",work(vv,2*(n-1)));
		else printf("0\n");
	}


} 
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2

*/ 
