#include<cstdio>
using namespace std;
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fe(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
const int N=1e5+5,M=1e9+7;
int T,n,m,V,id[N],a[N];
int ksm(int s,int p)
{
	int z=1;
	while(p>=1)
	{
		if(p&1) z=(ll)z*s%M;
		s=(ll)s*s%M,p>>=1;
	}
	return z;
}
void swap(int &o,int &p){int z=o;o=p,p=z;}
void kp(int l,int r)
{
	int i=l,j=r,mid=id[(l+r)>>1];
	while(i<=j)
	{
		while(id[i]<mid) ++i;
		while(id[j]>mid) --j;
		if(i<=j) swap(id[i],id[j]),swap(a[i],a[j]),++i,--j;
	}
	if(i<r) kp(i,r);
	if(l<j) kp(l,j);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&V);
		fo(i,1,m) scanf("%d%d",&id[i],&a[i]);
		kp(1,m);
		int ans=ksm((ll)V*V%M,id[1]-1+n-id[m]);
		fo(i,2,m)
		{
			if(id[i]==id[i-1]&&a[i]!=a[i-1])
			{
				ans=0;
				break;
			}
			if(id[i]>id[i-1]) ans=(ll)(ksm((ll)V*V%M,id[i]-id[i-1])-(ll)ksm(V,id[i]-id[i-1]-1)*(V-1)%M+M)%M*ans%M;
		}
		printf("%d\n",ans);
	}
	return 0;
}
/*
1
4 2 2
1 1
4 1
*/
