#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5,mod=1e9+7;
int T,n,m,v,cnt,b[N],bn,mp[N],t[N],ans,len;
bool f;
int read()
{
	int w=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') w=(w<<3)+(w<<1)+c-'0',c=getchar();
	return w*f;
}
struct node
{
	int c,d;
}a[N];
bool cmp(node a,node b)
{
	return a.c<b.c;
}
int qpow(int a,int b)
{
	int ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;
		b>>=1;
	}
	return ans;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	T=read();
	while(T--)
	{
		n=read();m=read();v=read();cnt=0;
		memset(t,0,sizeof t);f=0;
		for(int i=1;i<=m;i++) a[i].c=read(),a[i].d=read(),b[i]=a[i].c;
		sort(b+1,b+m+1);
		bn=unique(b+1,b+m+1)-b-1;
		sort(a+1,a+m+1,cmp);
		for(int i=1;i<=m;i++)
		{
			int tmp=lower_bound(b+1,b+bn+1,a[i].c)-b;
			if(t[tmp]>0&&t[tmp]!=a[i].d)
			{
				printf("0\n");
				f=1;break;
			}
			t[tmp]=a[i].d;mp[tmp]=a[i].c;
		}
		if(f) continue;
		ans=qpow(v,2*(mp[1]-1));
		for(int i=2;i<=bn;i++)
		{
			len=mp[i]-mp[i-1];
			if(len==0) ans=ans*((v-1)*v%mod+1)%mod;
			else ans=ans*((v-1)*qpow(v,2*len+1)%mod+1)%mod;
		}
//		ans=ans*((v-1)*qpow(v,))
		printf("%lld\n",ans);
	}
}
