#include<bits/stdc++.h>
using namespace std;
const int mod=1000000007;
long long ans=1,a[1145141],s[1145141];
long long n,m,v;
map <long long,long long> p;
long long qsm(long long x,long long c)
{
	if(c==0)
	  return 1;
	if(c==1)
	  return x;
	long long h=qsm(x,c/2)%mod;
	h=h*h%mod;
	if(c&1)
	  h=h*x%mod;
	return h;
}
void nahida()
{
	int f=0;
	p.clear();
	long long sum=0;
	for(int i=1;i<=m;i++)
	{
		long long c,d;
		scanf("%lld%lld",&c,&d);
		if(p[c]&&p[c]!=d)
		{
			printf("0\n");
			f=1;
		}
		if(f)
		  continue;
		if(p[c]&&p[c]==d)
		  continue;
		if(p[c-1])
		  sum++;
		if(p[c+1])
		  sum++;
		p[c]=d;
	}
	if(f)
	  return ;
	ans=(ans*qsm((v*(v-1)%mod+1)%mod,sum)%mod*qsm(v*v%mod,n-1-sum))%mod;
	printf("%lld\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;
	scanf("%d",&T);
	while(T)
	{
		T--;
		scanf("%lld%lld%lld",&n,&m,&v);
		memset(a,0,sizeof(a));
		memset(s,1,sizeof(s));
		ans=1;
		int f=0;
		nahida();
		continue;
	}
	return 0;
 } 
 /*
 I love Firefly.
 I love Nahida.
 CCF,helps me!
I shall ... set the seas ablaze.
 */
