#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+10;
const ll modd=1e9+7;
int m,m2,T;
ll n,v;
struct abc
{
	int p,val;
}q[maxn];
int c[maxn];
ll Pow(ll x,ll k)
{
	ll res=1;
	while(k)
	{
		if(k&1)	res=res*x%modd;
		x=x*x%modd;
		k>>=1;
	}
	return res;
}
ll jian(ll x,ll y){return x<y?x-y+modd:x-y;}
bool cmp(abc f1,abc f2){return f1.p==f2.p?f1.val<f2.val:f1.p<f2.p;}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%lld%d%lld",&n,&m,&v);
		for(int i=1,x;i<=m;i++)	scanf("%d%d",&q[i].p,&q[i].val);
		sort(q+1,q+1+m,cmp);
		bool f=1;
		for(int i=2;i<=m;i++)
		{
			if(q[i].p==q[i-1].p&&q[i].val!=q[i-1].val)	{f=0;break;}
		}
		if(!f)	{printf("0\n");continue;}
		m2=0;
		for(int i=1;i<=m;i++)
		{
			if(i==1||q[i].p!=q[i-1].p)	c[++m2]=q[i].p;
		}
		ll ans=1;
		ans*=Pow(v,2*(ll)(c[1]-1));
		for(int i=2;i<=m2;i++)
		{
			ll x=c[i]-c[i-1]-1;
			ans*=jian(Pow(v,2*(x+1)),(Pow(v,x)*(v-1))%modd),ans%=modd;
		}
		ans*=Pow(v,2*(ll)(n-c[m2])),ans%=modd;
		printf("%lld\n",ans);
	}
	return 0;
}
