//pass pretests on 9:27
//pass extreme on 9:31
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int read()
{
	char ch=getchar();int cna=0;
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9')cna=cna*10+ch-'0',ch=getchar();
	return cna;
}
const ll mod=1e9+7;
ll qp(ll x,ll y)
{
	ll s=1,base=x;
	while(y)
	{
		if(y&1)s=s*base%mod;
		base=base*base%mod;
		y>>=1;
	}
	return s;
}
int T,n,m;
ll v;
map<int,int>vis;
int p[100003],cnt;
void solve()
{
	vis.clear();
	n=read(),m=read(),v=read();
	int flag=0;cnt=0;
	for(int i=1;i<=m;i++)
	{
		int c=read(),d=read();
		if(vis[c]>0)
		{
			if(vis[c]!=d)flag=1;
		}
		else vis[c]=d,p[++cnt]=c;
	}
	if(flag){printf("0\n");return;}
	sort(p+1,p+cnt+1);
	ll ans=qp(v*v%mod,p[1]-1)*qp(v*v%mod,n-p[cnt])%mod;
	for(int i=1;i<cnt;i++)
	{
		ll x=qp(v*v%mod,p[i+1]-p[i])-qp(v,p[i+1]-p[i]-1)*(v-1)%mod;
		if(x<0)x+=mod;
		ans=ans*x%mod;
	}
	printf("%lld\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)solve();
	return 0;
}
