#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5,M=1e9+7;
struct Date{
	int x,i;
};Date d[N];
long long f[N];int a[N],b[N],ss;
long long Pow(long long x,int y)
{
	long long pre=1;
	while(y>0)
	{
		if((y&1)==1)
		pre=pre*x%M;
		x=x*x%M;y=y>>1;
	}
	return pre;
}
bool cmp(Date x,Date y)
{
	if(x.i<y.i)
	return 1;
	return 0;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T,n,m,i,x,y;long long K,p1,p2;bool pd;
	scanf("%d",&T);
	while(T>0)
	{
		T--;pd=0;
		scanf("%d%d%lld",&n,&m,&K);
		for(i=1;i<=m;i++)
		scanf("%d%d",&d[i].i,&d[i].x);
		sort(d+1,d+m+1,cmp);
		for(i=2;i<=m;i++)
		{
			if(d[i].i==d[i-1].i&&d[i].x!=d[i-1].x)
			pd=1;
		}
		if(pd==1)
		{
			printf("0\n");continue;
		}
		p1=K*K%M;
		f[1]=Pow(p1,d[1].i-1);
		for(i=2;i<=m;i++)
		{
			if(d[i].i==d[i-1].i)
			{
				f[i]=f[i-1];continue;
			}
			p2=Pow(K,d[i].i-d[i-1].i-1)%M;
			p2=p2*(K-1)%M;
			p2=(Pow(p1,d[i].i-d[i-1].i)-p2+M)%M;
			f[i]=f[i-1]*p2%M;
		}
		f[m]=f[m]*Pow(p1,n-d[m].i)%M;
		printf("%lld\n",f[m]);
	}
	return 0;
}
