#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
using namespace std;
const long long mod=1000000007;
long long t,n,m,v;
struct one
{
	long long c,d;
}a[110000];
long long f[110000],g[110000];
bool cmp(one x,one y)
{
	return x.c<y.c;
}
long long ksm(long long a,long long b)
{
	if(b<=0) return 1;
	long long k=ksm(a,b/2);
	if(b%2==0) return k*k%mod;
	return k*a%mod*k%mod;
}
long long dfs(long long p,long long x)
{
	if(g[p]!=0&&x!=g[p]) return 0;
	if(p==n) return 1;
	long long sum=0;
	for(int i=1;i<=v;i++)
	{
		sum+=dfs(p+1,i)*v;
	}
	return sum;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--)
	{
		bool fl=true;
		scanf("%lld%lld%lld",&n,&m,&v);
		for(int i=1;i<=m;i++)
		{
			scanf("%lld%lld",&a[i].c,&a[i].d);
		}
		sort(a+1,a+1+m,cmp);
		f[0]=1;
		for(int i=1;i<=m;i++)
		{
			if(a[i].c==a[i-1].c&&a[i].d!=a[i-1].d)
			{
				fl=false;
				printf("0\n");
				break;
			}
			g[a[i].c]=a[i].d;
			if(a[i].c==a[i-1].c)
			{
				f[i]=f[i-1];
				continue;
			}
			if(i==1&&a[i].c==1)
			{
				f[i]=1;
				continue;
			}
			f[i]=f[i-1]*ksm(v,a[i].c-a[i-1].c-1)%mod*ksm(1+v*(v-1)%mod,a[i].c-a[i-1].c)%mod;
		}
		if(fl&&v<=2)
		{
			printf("%lld\n",dfs(1,1)+(v==2?dfs(1,2):0));
			continue;
		}
		if(fl&&a[m].c==n) printf("%lld\n",f[m]);
		else if(fl) printf("%lld\n",f[m]*ksm(v*(1+v*(v-1)%mod)%mod,n-a[m].c-1)%mod);
	}
	return 0;
}
