#include<bits/stdc++.h>
using namespace std;
const long long mod = 1e9+7;
long long ans;
int T,n,m,v,c[100010],d[100010],x[10001],f[100100];
bool fg;
int vis[1000100];
long long binpow(long long a,long long b,long long w){
	long long res=1;
	while(b){
		if(b&1)res=res*a%w;
		a*=a%w;
		b>>=1;
	}
	return res;
}
void dfs(int dep,int *a){
	if(dep==n*2-1){
//		memcpy(f,x,sizeof(x));
		for(int i=1;i<=n;++i){
			f[i]=x[i];
		}
		for(int i=1;i<n;++i){
			if(f[i]>0){
				if(f[i]==a[i]){
					if(f[i+1]==0){
						f[i+1]=a[i+(n-1)];
					}else if(f[i+1]!=a[i+(n-1)]){
						return;
					}
				}
			}
		}
		ans++;
		return ;
	}
	for(int i=1;i<=v;++i){
		a[dep]=i;
		dfs(dep+1,a);
	}
}
//map<int,int>mp;
int a[1001];
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	while(T--){
		for(int i=1;i<=n;++i){
			vis[i]=0;
		}
//		mp.clear();
		fg=0;
		memset(x,0,sizeof(x));
		cin>>n>>m>>v;
		for(int i=1;i<=m;++i){
			cin>>c[i]>>d[i];
			x[c[i]]=d[i];
			if(vis[c[i]]&&vis[c[i]]!=d[i]){
				fg=1;
			}
//			if(mp[c[i]]!=0&&mp[c[i]]!=d[i]){
//				fg=1;
//			}
			vis[c[i]]=d[i];
//			mp.insert({c[i],d[i]});
//			cout<<mp[c[i]]<<" ";
		}
		if(fg){
			cout<<"0\n";
			continue;
		}
//		ans=binpow(v,n,mod);
//		cout<<ans<<"\n";
		if(n<=12&&m<=12&&v<=2){
			memset(a,0,sizeof(a));
			ans=0;
			dfs(1,a);
			cout<<ans<<"\n"; 
		}else if(1){
//			cout<<v<<" "<<(n-1)*2<<"\n";
			cout<<binpow(1ll*v,1ll*(n-1)*2,mod)<<"\n";
		}
	}
}


