#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
const int maxn=1e5;
int qpow(int x,long long l)
{
	int rtn=1;
	while(l)
	{
		if(l&1)rtn=1ll*rtn*x%mod;
		x=1ll*x*x%mod;
		l>>=1;
	}
	return rtn;
}
int t,n,m,v;
int ans=1;
struct node
{
	int val,id;
}a[maxn+5];
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout); 
	int i;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d",&n,&m,&v);
		for(i=1;i<=m;i++)scanf("%d%d",&a[i].id,&a[i].val);
		sort(a+1,a+m+1,[](node s1,node s2){return s1.id<s2.id||(s1.id==s2.id&&s1.val<s2.val);});
		a[0].id=1;a[0].val=a[1].val;
		ans=1;
		for(i=1;i<=m;i++)
		{
			if(a[i].id==a[i-1].id&&a[i].val!=a[i-1].val)
			{
				printf("0\n");
				goto end_;
			}
			if(a[i].id!=a[i-1].id)
			{
				if(i==1)ans=1ll*ans*qpow(v,2ll*(a[i].id-1))%mod;
				else ans=1ll*ans*(qpow(v,2ll*(a[i].id-a[i-1].id))-(1ll*qpow(v,a[i].id-a[i-1].id-1)*(v-1)%mod)+mod)%mod;
			}
		}
		ans=1ll*ans*qpow(v,2ll*(n-a[m].id))%mod;
		printf("%d\n",ans);
		end_:;
	}
	return 0;
}
