#include<bits/stdc++.h>
using namespace std;
int T,m,n,c[100005],d[100005],v,q[20],sum;
map<int,int> s;
long long mod=1e9+7; 
long long ksm(long long x,long long y)
{
	long long ans=1;
	while(y)
	{
		if(y&1) ans=ans*x%mod;
		y>>=1;
		x=x*x%mod;
	}
	return ans;
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin>>T;
	while(T--){
		cin>>n>>m>>v;
		long long ans=1;
		int a3=0,a1=0,a2=0;
		bool f=1;
		s.clear();
		int num=0;
		for(int i=1;i<=m;i++)
		{
			cin>>c[i]>>d[i];
			if(s[c[i]]!=0&&s[c[i]]!=d[i]) f=0; 
			if(s[c[i]]==d[i]) num++;
			s[c[i]]=d[i];
		} 
		if(!f){cout<<"0\n";continue;}
		if(m==1) 
		{
			cout<<ksm(v,2*n-2)<<'\n';
			continue;
		}
		sort(c+1,c+m+1); 
		unique(c+1,c+m+1);
		m-=num;
		for(int i=1;i<=m;i++)
		{
			if(c[i+1]!=c[i]+1) a1++;
			if(c[i+1]==c[i]+1) a2++;
			if(c[i-1]!=c[i]-1&&i!=1) a3++; 
		}
		int a0=n-1-a3-a1-a2;
		ans=ans*ksm(v*v%mod,a0+a1)%mod;
		ans=ans*ksm((v*v-v+1)%mod,a2+a3)%mod;
		cout<<ans<<'\n';
	}
	return 0;
}
