#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD=1e9+7;
int qpow(int a,int b,int p)
{
	a=a%p;
	int ret=1;
	while(b)
	{
		if(b&1)ret=(ret*a)%p;
		a=(a*a)%p;
		b>>=1;
	}
	return ret;
}
int jie[100001],invjie[100001];
map<int,int> mp;
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%lld",&T);
	jie[0]=1;
	invjie[0]=1;
	jie[1]=1;
	invjie[1]=1;
	for(int i=2;i<=100000;i++)
	{
		jie[i]=(jie[i-1]*i)%MOD;
		invjie[i]=qpow(jie[i],MOD-2,MOD);
	}
	for(int _i=1;_i<=T;_i++)
	{
		mp.clear();
		int n,m,v;
		scanf("%lld%lld%lld",&n,&m,&v);
		int flg=0;
		int mm=m;
		for(int i=1;i<=mm;i++)
		{
			int c,d;
			scanf("%lld%lld",&c,&d);
			if(mp[c]!=d&&mp[c]!=0)
			{
				flg=1;
			}
			if(mp[c]==d)m--;
			mp[c]=d;
		}
		if(flg)
		{
			printf("0\n");
			continue;
		}
		int ans=0;
		for(int i=0;i<m;i++)
		{
			if(i%2==0)ans+=((qpow(v,2*n-2-2*i,MOD)*qpow(v-1,i,MOD))%MOD*(((jie[m-1]*invjie[i])%MOD*invjie[m-i-1])%MOD))%MOD;
			else ans-=((qpow(v,2*n-2-2*i,MOD)*qpow(v-1,i,MOD))%MOD*(((jie[m-1]*invjie[i])%MOD*invjie[m-i-1])%MOD))%MOD;
			//if(_i==4)cout<<((qpow(v,2*n-2-2*i,MOD)*qpow(v-1,i,MOD))%MOD*(((jie[m-1]*invjie[i])%MOD*invjie[m-i-1])%MOD))%MOD<<'\n';
			ans=((ans%MOD)+MOD)%MOD;
		}
		printf("%lld\n",ans);
	}
	return 0;
}

