# include <cstdio>
# include <algorithm>
# define int long long
# define mod 1000000007
using namespace std;
int T,n,m,k,a[300007],b[300007],sum;
int pow(int x,int y)
{
	int ans=1;
	while(y)
	{
		if(y%2==1) ans=ans*x%mod;
		x=x*x%mod;
		y/=2;
	}
	return ans;
}
void qsort(int x,int y)
{
	int i=x,j=y,mid=a[(x+y)/2];
	while(i<=j)
	{
		while(a[i]<mid) i++;
		while(a[j]>mid) j--;
		if(i<=j)
		{
			swap(a[i],a[j]);
			swap(b[i],b[j]);
			i++,j--;
		}
	}
	if(i<y) qsort(i,y);
	if(x<j) qsort(x,j);
}
int read()
{
	char ch=' ';
	int ans=0;
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar();
	return ans;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	for(int happy=1;happy<=T;happy++)
	{
		scanf("%lld%lld%lld",&n,&m,&k);
		for(int i=1;i<=m;i++) a[i]=read(),b[i]=read();
		qsort(1,m);
		sum=1;
		for(int i=2;i<=m;i++)
		{
			if(a[i]==a[i-1]&&b[i]!=b[i-1])
				sum=0;
		}
		if(sum==0)
		{
			printf("0\n");
			continue;
		}
		for(int i=2;i<=m;i++)
		{
			if(a[i]==a[i-1]) continue;
			int p=a[i]-a[i-1];
			sum=sum*(pow(k,2*p)-pow(k,p-1)*(k-1)%mod)%mod;
			sum=(sum+mod)%mod;
		}
		if(a[1]!=1)
			sum=sum*pow(k,2*(a[1]-1))%mod;
		if(a[m]!=n)
			sum=sum*pow(k,2*(n-a[m]))%mod;
		printf("%lld\n",sum);
	}
	return 0;
}
