#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod=1e9+7;
const int N=1e5+100;
int ans;
int a[20],b[20];
map<int,int> mp;
int T,n,m,v;
int qpow(int a,int b){
	int res=1;
	int base=a;
	while(b){
		if(b&1) (res*=base)%=mod;
		b>>=1;
		(base*=base)%=mod;
	}
	return res%mod;
}
bool check(){
	for(int i=1;i<n;++i){
		if(mp[i]&&mp[i+1]&&a[i]==mp[i]&&b[i]!=mp[i+1]) return false;
	}
	return true;
}
void dfs(int x){
	if(x>n-1){
		if(check()) ++ans;
		return;
	}
	a[x]=1,b[x]=1;
	dfs(x+1);
	a[x]=2;
	dfs(x+1);
	b[x]=2;
	dfs(x+1);
	a[x]=1,b[x]=2;
	dfs(x+1);
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>T;
	while(T--){
		mp.clear();
		cin>>n>>m>>v;
		bool flag=0;
		for(int i=1;i<=m;++i){
			int x,y;
			cin>>x>>y;
			if(mp[x]&&mp[x]!=y) flag=1;
			mp[x]=y;
		}
		if(flag) {
			cout<<0<<endl;
			continue;
		}
		if(m==1){
			cout<<qpow(v,n-1)%mod*qpow(v,n-1)%mod<<endl;
		}
		else if(n<=12&&v<=2){
			dfs(1);
			cout<<ans%mod<<endl;
		}
	}
	return 0;
}
