#include<bits/stdc++.h>
using namespace std;
#define N 100005
const int p=1e9+7;
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9')
	{
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9')
	{
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	return x*f;
}
int T,n,m,v,x,a[N],ans;
map<int,int> mp;
inline int quickpow(int a,int b)
{
	int res=1;
	while(b)
	{
		if(b&1) res=1ll*res*a%p;
		a=1ll*a*a%p;
		b>>=1;
	}
	return res;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		mp.clear(),ans=1;
		scanf("%d%d%d",&n,&m,&v);
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&a[i],&x);
			if(mp[a[i]]&&mp[a[i]]!=x)
			{
				ans=0;
				goto L;
			}
			mp[a[i]]=x;
		}
		sort(a+1,a+m+1);
		m=unique(a+1,a+m+1)-a-1;
		for(int i=2;i<=m;i++) ans=1ll*ans*((quickpow(v,2ll*(a[i]-a[i-1])%(p-1))-1ll*(v-1)*quickpow(v,(a[i]-a[i-1]-1)%(p-1))%p)%p+p)%p;
		ans=1ll*ans*quickpow(v,2ll*(a[1]-1)%(p-1))%p*quickpow(v,2ll*(n-a[m])%(p-1))%p;
		L:printf("%d\n",ans);
	}
	return 0;
}
