//
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=1e5+5,mod=1e9+7;

int c[MAXN],d[MAXN],a[MAXN];

int qpow(int a,int b){
	int res=1;
	while (b>0){
		if (b&1) res=(res*a)%mod;
		a%=mod;
		a*=a;
		a%=mod;
		b/=2; 
	}
	return res%mod;
}

signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	scanf("%lld",&t);
	while (t--){
		memset(a,0,sizeof(a)); 
		memset(c,0,sizeof(c)); 
		memset(d,0,sizeof(d)); 
		int n,m,v;
		scanf("%lld %lld %lld",&n,&m,&v);
		bool fg=(n==m),fg1=0;
		for (int i=1;i<=m;i++){
			scanf("%lld %lld",&c[i],&d[i]);
			if (c[i]!=i){
				fg=0;
			}
			if (a[c[i]] and a[c[i]]!=d[i]){
				printf("0\n");
				fg1=1;
				break;
			}
			a[c[i]]=d[i];
		}
		if (fg1) continue;
		if (fg){
			int tmp=v;
			tmp--;
			tmp%=mod;
			tmp*=tmp;
			tmp%=mod;
			tmp++;
			tmp%=mod;
			printf("%lld\n",qpow(tmp,n-1));
		}
		else{
			int ans=0;
			for (int i=0;i<(1<<(2*n-2));i++){
				bool fg=1;
				for (int j=1;j<n;j++){
					if (a[j]==((i>>(2*(j-1)))&1)+1 and a[j+1]!=((i>>(2*(j-1)+1))&1)+1 and a[j+1]!=0){
						fg=0;
						break;
					}
				}
				ans+=fg;
			}
			printf("%lld\n",ans);
		}
	}

	return 0;
}

