#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int M=1e5+4;
const int MOD=1e9+7;
ll t,n,m,v,ans;
struct node{
	ll c,d;
}a[M];
bool cmp(node x,node y){
	return x.c<y.c;
}
ll quick_power(ll base,ll power){
	ll result=1;
	while(power>0){
		if(power&1){
			result*=base;
			result%=MOD;
		}
		power>>=1;
		base*=base;
		base%=MOD;
	}
	return result;
}
bool more(){
	for(int i=2;i<=m;i++){
		if(a[i].c==a[i-1].c&&a[i].d!=a[i-1].d){
			return true;
		}
	}
	return false;
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--){
		cin>>n>>m>>v;
		ans=0;
		for(int i=1;i<=m;i++){
			cin>>a[i].c>>a[i].d;
		}
		if(m==1){
			cout<<quick_power(v,(n-1)*2)<<'\n';
			continue;
		}
		sort(a+1,a+m+1,cmp);
		if(more()){
			cout<<"0\n";
			continue;
		}
		ans=quick_power(v,(n-1)*2);
		for(int i=2;i<=m;i++){
			ans-=quick_power(v,(n-2)*2);
			ans=(ans+MOD)%MOD;
		}
		cout<<ans<<'\n';
	}
	return 0;
}
