#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
long long n,m,v;
struct node
{
	int i,v; 
}a[300001];
int opp[300001];
long long M=1000000007;
long long u[300001];
long long pow(long long a,long long b)
{
	int ans=1;
	while(b)
	{
		if(b&1)ans=ans*a%M;
		a=a*a%M;
		b>>=1;
	}
	return ans;
}
bool cmp(node a,node b){return a.i<b.i;}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	cin>>t;
	while(t--)
	{
		long long ans;
		cin>>n>>m>>v;
		int op,kl=0;
		for(int i=0;i<m;i++)
			scanf("%d%d",&a[i].i,&a[i].v);
		sort(a,a+m,cmp);
		kl=a[m-1].i-a[0].i;
		bool flag=0;
		for(int i=1;i<m;i++)if(a[i].i==a[i-1].i&&a[i].v!=a[i-1].v)flag=1;
		if(flag)
		{
			cout<<"0\n";
			continue;
		}
		long long mi=1;
		ans=pow(v,2*n-2);
		long long ans1=1,ans2=1,ans3=1,ans4=1;
//		cout<<ans<<"\n";
		for(int i=1;i<m;i++)
		{
			if(a[i].i==a[i-1].i)continue;
			u[i]=pow(v,a[i].i-a[i-1].i-1)*(v-1)%M;
			ans1=ans1*(((pow(v,(a[i].i-a[i-1].i)*2)-u[i])%M+M)%M)%M;
		}
		cout<<ans1*pow(v,(n-kl-1)*2)%M<<"\n";
	}
	return 0;
}
