#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
using namespace std;
#define mod 1000000007
int vt[100010];
int a[100010],tot;
long long ksm(long long k,int a)
{
	long long sum=k;
	long long ans=1;
	while(a)
	{
		
		if(a%2==1)
		ans*=sum;
		ans%=mod;
		sum=sum*sum;
		sum%=mod;
		a/=2;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	long long n,m,v;
	cin>>t;
//	cout<<ksm(3,0)<<endl;
	while(t--)
	{
		int lc,ld;
		int flag=1;
		cin>>n>>m>>v;
		memset(vt,0,sizeof(vt));
		memset(a,0,sizeof(a));
		tot=0;
		for(int i=1;i<=m;i++)
		{
			cin>>lc>>ld;
			if(vt[lc]!=0&&vt[lc]!=ld&&flag==1)
			{
				cout<<0<<endl;
				flag=0;
			}
			if(vt[lc]==0)a[++tot]=lc;
			vt[lc]=ld;
		}
		if(flag==0)continue;
		sort(a+1,a+tot+1);

		long long ans=1;
		for(int i=2;i<=tot;i++)
		{
			long long sum;
			sum=ksm(v*v,a[i]-a[i-1])+mod-ksm(v*v,a[i]-a[i-1]-2);
	//		cout<<sum<<" ";
		sum%=mod;
			ans*=sum;
			ans%=mod;
		}
		ans*=ksm(v*v,a[1]-1);
	//	ans%=mod;
		ans*=ksm(v*v,n-a[tot]);
	//	ans%=mod;
		cout<<ans%mod<<endl;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}
