#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7,N=100010;
int n,m,v,T,vis[N],lun,a[N],p[2*N],w[N],ans;
map<int,pair<int,int> > t;
int POW(int a,int b){
	int res=1;
	while(b){
		if(b&1) res=1ll*res*a%mod;
		a=1ll*a*a%mod;
		b>>=1;
	}
	return res;
}
void dfs(int x){
	if(x==n){
		for(int i=1;i<=n;i++) w[i]=a[i]*(vis[i]==lun);
		for(int i=1;i<n;i++){
			if(!w[i]) continue;
			if(w[i]==p[2*i-1]){
				if(w[i+1]!=p[2*i]&&vis[i+1]==lun) return;
				w[i+1]=p[2*i];
			}
		}
		ans++;
		return;
	}
	p[2*x-1]=p[2*x]=1;//1 1
	dfs(x+1);
	p[2*x]=2;//1 2
	dfs(x+1);
	p[2*x-1]=2;//2 2
	dfs(x+1);
	p[2*x]=1;// 2 1
	dfs(x+1);
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		lun++;bool ok=false;
		scanf("%d%d%d",&n,&m,&v);
		if(n<=12&&v==2){
			for(int i=1;i<=m;i++){
				int c,d;
				scanf("%d%d",&c,&d);
				if(vis[c]!=lun||(vis[c]==lun&&a[c]==d)) vis[c]=lun,a[c]=d;
				else ok=true;
			}
			if(ok) printf("0\n");
			else{
				ans=0;
				dfs(1);
				printf("%d\n",ans);
			}
		}else if(m==1){
			for(int i=1;i<=m;i++){
				int c,d;
				scanf("%d%d",&c,&d);
				if(t[c].first!=lun||(t[c].first==lun&&t[c].second==d)) t[c].first=lun,t[c].second=d;
				else ok=true;
			}
			if(ok) printf("0\n");
			else printf("%d\n",POW(v,2*n-2));
		}else printf("0\n");
	}
	return 0;
}
