#include<bits/stdc++.h>
using namespace std;
const int M=1e5+13;
const int mod=1e9+7;
int t;
int n,m,v;
int x[M];
int d[M],c[M];
int a[M],b[M];
long long ans;
void D()
{
	ans=1;
	for(int i=1;i<n;i++)
	{
		if(x[i]&&!x[i+1]) ans=ans*v*v,x[i+1]=1; 
		else 
			if(x[i]&&x[i+1]) ans=ans*(v*(v-1)+1);
			else
				if(!x[i]&&x[i+1]) ans=ans*(2*v*(v-1)+1);
				else
					if(!x[i]&&!x[i+1]) ans=ans*v*v*v,x[i+1]=1;
		ans%=mod;
	}
	printf("%lld\n",ans);
}
int main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
	{
		memset(x,0,sizeof(x));
		cin>>n>>m>>v;
		for(int i=1;i<=m;i++)
			scanf("%d%d",&c[i],&d[i]);
		ans=1;
		for(int i=1;i<=m;i++)
		{
			if(x[c[i]])
			{
				ans=0;
				break;	
			}
			x[c[i]]=d[i];	
		}
		if(ans==0) 
			cout<<ans<<endl;
		else D(); 		
	}
	return 0;
} 
