#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int inf = (int)1e9+7; 
long long dp[N],n,m,v,t;
long long a[N];
//10:05
//wo kao 
//you ge ren bi sai bi dao yi ban tu ran dao zai di shang chou chu
//wo kao
//xia si wo le
//tai kong bu le 
long long vis[N];
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--)
	{
		scanf("%lld%lld%lld",&n,&m,&v);
		memset(vis,0,sizeof vis);
		memset(dp,0,sizeof dp);
		memset(a,0,sizeof a);
		bool ff=0;
		for(int i=1;i<=m;i++) 
		{
			long long x,y;
			scanf("%lld%lld",&x,&y);
			if(vis[x]!=0 && vis[x]!=y)
			{
				ff=1;
				puts("0");
				break;
			}
			a[x]=y;
			vis[x]=y;
		}
		if(ff) continue;
		long long ans=1;
		for(int i=2;i<=n;i++)
		{
			if(a[i]==0 || a[i-1]==0) dp[i]=v*v;
			else dp[i]=v*v-v;
			ans*=dp[i]%inf;ans%=inf;
		}
		printf("%lld\n",ans);
	} 
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2

*/
