#include<bits/stdc++.h>
#define Yukinoshita namespace
#define Yukino std
using Yukinoshita Yukino;
int read()
{
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') w=ch=='-'?-1:1,ch=getchar();
	while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int mod=1e9+7;
int qpow(int a,int p)
{
	int mul=1;
	for(;p;p>>=1)
	{
		if(p&1) mul=1ll*mul*a%mod;
		a=1ll*a*a%mod; 
	}
	return mul;
}
inline void getmod(int &x)
{
	x-=(x>=mod)*mod;
}
const int mxn=1e5+5;
pair<int,int> a[mxn];
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T=read();
	while(T--)
	{
		int n=read(),m=read(),v=read(),i;
		for(i=1;i<=m;i++)
			a[i].first=read(),a[i].second=read();
		sort(a+1,a+1+m);
		bool fl=0;
		for(i=1;i<m;i++)
			if(a[i].first==a[i+1].first&&a[i].second!=a[i+1].second)
			{
				fl=1;
				puts("0");
				break;
			}
		if(fl) continue;
		int mul=1;
		for(i=1;i<m;i++)
			if(a[i].first!=a[i+1].first)
				mul=1ll*mul*(qpow(v,(a[i+1].first-a[i].first)*2)-1ll*qpow(v,a[i+1].first-a[i].first-1)*(v-1)%mod+mod)%mod;
		mul=1ll*mul*qpow(v,(a[1].first-1)*2)%mod;
		mul=1ll*mul*qpow(v,(n-a[m].first)*2)%mod;
		printf("%d\n",mul);
	}
}
