#include<bits/stdc++.h>
#define M 1000001
#define ll long long
using namespace std;
inline ll read()
{
	ll k=0,f=0;char c=getchar();
	for(;!isdigit(c);c=getchar()) f|=c=='-';
	for(;isdigit(c);c=getchar()) k=(k<<1)+(k<<3)+(c^48);
	return f?-k:k;
}
inline void write(ll x)
{
	if(x<0) putchar('-'),x=-x;
	ll stk[35],top=0;
	stk[top++]=x%10,x/=10;
	while(x) stk[top++]=x%10,x/=10;
	while(top) putchar(stk[--top]+48);
	puts("");
}
const ll mod=1e9+7;
ll T,n,m,len,v,ans,c[M];
struct node
{
	ll C,D;
}w[M];
inline bool cmp(node x,node y)
{
	return x.C<y.C;
}
inline void init()
{
	for(ll i=1;i<=len;i++) c[i]=0;
	sort(w+1,w+m+1,cmp),len=0;
	for(ll i=1;i<m;i++)
	{
		if(w[i].C==w[i+1].C&&w[i].D!=w[i+1].D)
		{
			ans=-1;
			return;
		}
	}
	for(ll i=1;i<=m;i++)
	{
		if(c[len]!=w[i].C) c[++len]=w[i].C;
	}
	return;
}
inline ll ksm(ll x,ll y)
{
	ll z=1;
	while(y)
	{
		if(y&1) z=z*x%mod;
		x=x*x%mod,y>>=1;
	}
	return z;
}
inline ll calc(ll x,ll y)
{
	ll A=(v-1)*ksm(v,y-x-1)%mod,B=ksm(v*v%mod,y-x)%mod;
	return ((B-A+mod)%mod+mod)%mod;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--)
	{
		n=read(),m=read(),v=read(),ans=0;
		for(ll i=1;i<=m;i++) w[i].C=read(),w[i].D=read();
		init();
		if(ans==-1)
		{
			puts("0");
			continue;
		}
		ans=ksm(v*v%mod,c[1]-1);
		for(ll i=1;i<len;i++) ans=(ans*calc(c[i],c[i+1]))%mod;
		ans=(ans*ksm(v*v%mod,n-c[len]))%mod;
		printf("%lld\n",ans);
	}
	return 0;
}

