#include<bits/stdc++.h>
using namespace std;
const int p=1000000007;
int t,n,m,v,cnt;
int read()
{
	char c=getchar();
	while(c<'0'||c>'9') c=getchar();
	int ret=0;
	while(c>='0'&&c<='9') ret=(ret<<1)+(ret<<3)+c-'0',c=getchar();
	return ret;
}
struct M
{
	int c,d;
}a[100010],aa[100010];
bool cmp(M x,M y)
{
	if(x.c!=y.c)return x.c<y.c;
	return x.d<y.d;
}
long long ksm(long long x,long long y)
{
	long long ret=1,now=x;
//	cout<<y;
	while(y)
	{
		if(y&1) ret=ret*now%p;
		now=now*now%p;
		y>>=1;
		
	}
	return ret;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&t);
	while(t--)
	{
		cnt=0;
		memset(aa,0,sizeof(aa));
		n=read(),m=read(),v=read();
		for(int i=1;i<=m;i++)
		{
			a[i].c=read(),a[i].d=read();
		}
		sort(a+1,a+m+1,cmp);
		bool flag=0;
		for(int i=1;i<=m;i++)
		{
			if(a[i].c==a[i+1].c&&a[i].d!=a[i+1].d)
			{
				flag=1;
				break;
			}
			if(a[i].c!=a[i+1].c)
			{
				aa[++cnt].c=a[i].c,aa[cnt].d=a[i].d;
			}
		}
		if(flag)
		{
			printf("0\n");
			continue;
		}
		long long ans=1;
		ans=ans*ksm(v,1LL*2*(aa[1].c-1+n-aa[cnt].c))%p;
		for(int i=1;i<cnt;i++)
		{
			int kk=aa[i+1].c-aa[i].c;
			if(kk==1)
			{
				ans=ans*(1LL*v*v%p-v+1)%p;
			}
			else
			{
				long long sum=1,sum2=1;
				sum=sum*ksm(v,1LL*kk*2)%p;
				sum2=sum2*ksm(v,kk-1)%p*(v-1)%p;
				ans=(sum+p-sum2)%p*ans%p;
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}
