#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
using namespace std;
const ll mod=1e9+7;
int n,m,v;
int a[1000005];
bool flag=0;
ll ans;
void init()
{
	scanf("%d%d%d",&n,&m,&v);
	memset(a,0,sizeof(a));
	ans=1;
	flag=0;
	for(int i=1;i<=m;i++)
	{
		int c,j;
		scanf("%d%d",&c,&j);
		if(a[c]!=j&&a[c]!=0)
		flag=1;
		a[c]=j;
	}
}
void work()
{
	if(flag)
	{
		printf("0\n");
		return;
	}
	ll uns=1,su=0;
	for(int i=2;i<=n;i++)
	{
		if(a[i-1]!=0&&a[i]!=0)
		{
			ans*=(1+(v-1)%mod*v%mod);
		}
		else if(a[i-1]!=0&&a[i]==0)
		{
			su=v;
			uns=(v-1)%mod*v%mod;
		}
		else if(a[i-1]==0&&a[i]==0)
		{
			uns=uns*v%mod*v%mod+su*(v-1)%mod*v%mod;
			su=su*v%mod;
		}
		else if(a[i-1]==0&&a[i]!=0)
		{
			su=su*1;
			uns=v*v%mod*uns%mod;
			ans*=(su*(1+(v-1)*v%mod)%mod+uns)%mod;
			ans%=mod;
		}
		if(i==n&&!(a[i-1]==0&&a[i]!=0)&&!(a[i-1]!=0&&a[i]!=0))
		ans*=(su+uns)%mod,ans%=mod;
	}
	printf("%lld\n",ans%mod);
}
int main()
{
	int T;
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		init();
		work();
	}
	return 0;
}
