#include<bits/stdc++.h>
#define int long long
const int mod=1000000007;
using namespace std;
int t,n,m,v,ans;
struct data{
	int c,d;
}a[100010];
int x[1000010];
int dp[1000010],sum[1000010];
bool cmp(data ma,data mi){
	return ma.c<mi.c;
}
int qpow(int x,int y){
	int res=1;
	while(y){
		if(y&1)res=res*x,res%=mod;
		x=x*x,x%=mod;
		y>>=1;
	}
	return res;
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		memset(x,0,sizeof(x));
		memset(dp,0,sizeof(dp));
		memset(sum,0,sizeof(sum));
		ans=0;
		for(int i=1;i<=m;i++)
			scanf("%lld%lld",&a[i].c,&a[i].d);
		if(m==1){
			printf("%lld\n",qpow(v*v%mod,n-1));
		}
		else{
			for(int i=1;i<=m;i++){
				if(x[a[i].c]!=0&&x[a[i].c]!=a[i].d){
					ans=-1;
					break;
				}
				x[a[i].c]=a[i].d;
			}
			if(ans==-1)printf("0\n");
			else{
				ans=1;
				for(int i=1;i<n;i++){
					if(x[i]&&x[i+1]){
						ans*=(((v-1)*v%mod)+1)%mod; 
					}
					else ans*=v*v%mod;
					ans%=mod;
				}
				printf("%lld\n",ans);
			}
		}
	}
	return 0;
}
