#include<bits/stdc++.h>
using namespace std;
#define N 200005
#define mod 1000000007
int n,m,v;
struct LIM{int c,d;}a[N],b[N];
inline int qpow(int x,int y){int ret=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ret=1ll*ret*x%mod;return ret;}
inline int del(int x,int y){return x-y<0?x-y+mod:x-y;}
void solve()
{
	scanf("%d%d%d",&n,&m,&v);
	for(int i=1;i<=m;i++)
		scanf("%d%d",&a[i].c,&a[i].d);
	sort(a+1,a+m+1,[&](LIM x,LIM y){return x.c<y.c;});
	int k=0;
	for(int i=1;i<=m;i++)
	{
		if(b[k].c==a[i].c)
		{
			if(b[k].d!=a[i].d)
				return printf("0\n"),void();
		}
		else
			b[++k]=a[i];
	}
	int ans=1ll*qpow(v,2*(b[1].c-1))*qpow(v,2*(n-b[k].c))%mod;
	for(int i=1;i<k;i++)
		ans=1ll*ans*del(qpow(v,2*(b[i+1].c-b[i].c)),1ll*qpow(v,b[i+1].c-b[i].c-1)*(v-1)%mod)%mod;
	printf("%d\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;scanf("%d",&T);
	while(T--) solve();
} 
