#include<bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
const int M = 1e9+5;
const int mod = 1e9+7;
int T;
long long n,m,v;
unordered_map<int,int>mp;
namespace st1{
	long long ans;
	int a[30];
	void dfs(int u){
		if(u==2*n-2){
			int lst = 0;
			int flag = 1;
			for(int i = 1;i<n;++i){
				if(lst){
					if(mp[i]){
						if(mp[i]!=a[i+n-1]){
							flag = 0;
							break;
						}else{
							if(a[i]!=mp[i]){
								lst = 0;
							}
						}
					}else{
						if(a[i]!=a[i-1+n-1])lst=0;
					}
				}else{
					if(mp[i]&&mp[i]==a[i]){
						lst=1;
					}
				}
			}
			if(lst){
				if(mp[n]&&mp[n]!=a[2*n-2])flag = 0;
			}
			if(flag){
				++ans;
				ans%=mod;
			}
			return ;
		}
		++u;
		a[u]=1;
		dfs(u);
		a[u]=2;
		dfs(u);
	}
	void solve(){
		ans = 0;
		dfs(0);
		printf("%lld\n",ans);
	}
}
namespace st2{
	long long ksm(long long x,long long y){
		long long res = 1;
		x%=mod;
		while(y){
			if(y&1){
				res*=x;
				res%=mod;
			}
			x*=x;
			x%=mod;
			y>>=1;
		}
		return res;
	}
	void solve(){
		long long x = v%mod*v%mod;
		long long y = n-1;
		printf("%lld\n",ksm(x,y)); 
	}
}
namespace st3{
	void solve(){
		long long x = (v-1)%mod*v%mod;
		long long res = 1;
		long long ans = 1;
		for(int i=1;i<=n-1;++i){
			res*=x;
			res%=mod;
			ans+=res*(n-1)%mod;
			ans%=mod;
		}
		printf("%lld\n",ans);
	}
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%d",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v);
		mp.clear();
		int flag = 0;
		for(int i = 1;i<=m;++i){
			int c,d;
			scanf("%d%d",&c,&d);
			if(mp[c]!=0&&mp[c]!=d){
				flag = 1;
			}
			mp[c]=d;
		}
		if(flag){
			printf("0\n");
			continue;
		}
		if(n==1){
			printf("%lld\n",v*v%mod);
			continue;
		}
		//cout<<"A"<<endl;
		if(m==1){
			st2::solve();
		}else if(n<=12&&m<=12&&v<=2){
			st1::solve();
		}else{
			st3::solve();
		}
	}
	return 0;
}
