#include<iostream> 
#include<stdio.h>
#include<string.h>
using namespace std;
const int N=1e7+1,mod=1e9+7;
struct node{
	long long x,y;
}ax[N];
struct edge{
	long long st,ed;
}bx[N];
long long z,a,b,c,x,y,buc[N],f[N][2],tmp;
bool flag,ctrl[N];
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&z);
	while(z--)
	{
		memset(ctrl,false,sizeof(ctrl));
		flag=false;
		memset(buc,0,sizeof(buc));
		scanf("%lld%lld%lld",&a,&b,&c);
		for(int i=1;i<=b;i++)
		{
			scanf("%lld%lld",&x,&y);
			if(buc[x]!=0&&buc[x]!=y)flag=true;
			else buc[x]=y;
			ctrl[x]=true;
			if(flag)continue;
		}
		if(flag)
		{
			puts("0");
			continue;
		}
		f[0][0]=1;
		f[0][1]=0;
		tmp=1;
		for(int i=1;i<a;i++)
		{
			if(ctrl[i])
			{
				f[i][0]=(f[i-1][0]*c%mod)*(c-1)%mod+(f[i-1][1]*c%mod)*(c-1)%mod;
				if(ctrl[i+1])f[i][1]=1;
				else f[i][1]=c;
			}else{
				if(ctrl[i-1]&&ctrl[i+1])
				{
					f[i][0]=(f[i-1][0]*c%mod)*c%mod+(f[i-1][1]*c%mod)*(c-1)%mod;
					f[i][1]=1;
				}else{
					f[i][0]=((f[i-1][0]+f[i-1][1])*c%mod)*c%mod;
					f[i][1]=0;
				}
			}
		}
		printf("%lld\n",(f[a-1][0]+f[a-1][1])%mod);
	}
}
