#include<bits/stdc++.h>
using namespace std;
#define int long long
bool Mst;
const int N = 1e6+10,MOD = 1e9+7;
int T,n,m,u; 
int c[N],d[N];
unordered_map<int,int> x,a,b;
bool Med;
int ksm(int x,int y)
{
	int res = 1;
	while(y)
	{
		if(y&1)
			res = (res*x)%MOD;
		y>>=1;
		x = (x*x)%MOD;
	}
	return res;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	fprintf(stderr,"%.2lfMB\n",abs(&Med-&Mst)/1024576.0);
	scanf("%lld",&T);
	while(T--)
	{
		bool f = 1;
		scanf("%lld%lld%lld",&n,&m,&u);
		for(int i = 1;i<=m;i++)
			scanf("%lld%lld",&c[i],&d[i]);
		if(m==1)
		{
			printf("%lld\n",ksm(u*u%MOD,n-1));
			continue;
		}
		for(int i = 1;i<=n;i++)
			x[i] = 0;
		for(int i = 1;i<=m;i++)
		{
			if(x[c[i]])
			{
				f = 0;
				break;
			}
			x[c[i]] = d[i];
		}
		if(!f)
		{
			puts("0");
			continue;
		}
		if(m==n)
		{
			printf("%lld\n",ksm((u*u-u+1)%MOD,n-1));
			continue;
		}
	}
	return 0;
}
