#include<bits/stdc++.h>
#define ll long long
#define R register int
#define if(x) if(__builtin_expect(x,1))
#define while(x) while(__builtin_expect(x,1))
using namespace std;
const int N=1e5+1,mod=1e9+7;
int n,m,v,T;map<int,int>a;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=res*1ll*a%mod;
		b>>=1;a=a*1ll*a%mod;
	}
	return res;
}
void work(){
	bool f1,f2,f3,f4;f1=f2=f3=f4=1;
	for(R i=1; i<n; i++)
		if(a[i]&&a[i+1]){
			if(a[i]==1&&a[i+1]!=1){f1=0;break;}
			if(a[i]==1&&a[i+1]!=2){f2=0;break;}
			if(a[i]==2&&a[i+1]!=1){f3=0;break;}
			if(a[i]==2&&a[i+1]!=2){f4=0;break;}
		}
	if(!f1&&!f2&&!f3&&!f4)return cout<<"0\n",void();
	ll ans=0;int k=f1+f2+f3+f4;
	if(k)ans=k;
	if(k>=2)ans=(1ll*k*(k-1)*qpow(2,n-1)%mod)%mod;
	if(k>=3)ans=(1ll*k*(k-1)*(k-2)*qpow(3,n-1)%mod)%mod;
	if(k>=4)ans=(qpow(4,n-1))%mod;
	cout<<ans<<'\n';
}
void solve(){
	a.clear();
	cin>>n>>m>>v;bool fl=1;
	for(R i=1,x,y; i<=m; i++){
		cin>>x>>y;
		if(a[x]!=y&&a[x])fl=0;
		a[x]=y;
	}
	if(!fl)return cout<<"0\n",void();
	if(m==1)return cout<<qpow(v*1ll*v%mod,n-1)<<'\n',void();
	if(v<=2)work();
}
int 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--)solve();
	return 0;
}

