#include<bits/stdc++.h>
using namespace std;
const int P=1e9+7;
int T,fl,n,m,V,ans;
map<int,int>mp;
int pown(int x,int y)
{
	int s=1;
	for(int i=0;i<31;i++)
	{
		if(y>>i&1)
			s=1LL*s*x%P;
		x=1LL*x*x%P;
	}
	return s;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&V);
		mp.clear(),fl=0;
		for(int i=1,x,y;i<=m;i++)
		{
			scanf("%d%d",&x,&y);
			if(mp.find(x)!=mp.end()&&mp[x]!=y)
				fl=1;
			mp[x]=y;
		}
		if(fl)
		{
			puts("0");
			continue;
		}
		ans=1;
		for(auto i:mp)
		{
			if(fl)
				ans=1LL*ans*(pown(V,i.first-fl<<1)+P-pown(V,i.first-fl-1)*(V-1LL)%P)%P;
			else
				ans=pown(V,i.first-1<<1);
			fl=i.first;
		}
		printf("%lld\n",1LL*ans*pown(V,n-fl<<1)%P);
	}
}
