#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=1e9+7,M=1.1e5;
ll qpow(ll x,ll k) {
	x%=mod;
	ll ans=1,a=x;
	while(k) {
		if(k%2) ans=ans*a%mod;
		a=a*a%mod; k/=2;
	}
	return ans;
}
struct node {
	ll a,b;
}A[M],B[M];
void solve() {
	ll n,m,v;
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++) {
		cin>>A[i].a>>A[i].b;
	}
	sort(A+1,A+1+m,[&](node x,node y) {return x.a<y.a;});
	int cnt=0;
	for(int i=2,j=1;i<=m;i++) {
		if(A[i].a==A[i-1].a) {
			if(A[i].b!=A[i-1].b) {
				cout<<0<<endl;return ;
			}
			else {
				cnt++;
			}
		} else {
			B[++j]=A[i];
		}
	}
	m-=cnt;
	for(int i=2;i<=m;i++) {
		A[i]=B[i];
	}
	if(m==1) {
		ll ans=0;
		cout<<qpow(v,n*2-2);
	}
	else if(m==n) {
		ll ans=0;
		cout<<qpow(1ll*v*v-v+1,n-1);
	}
	else {
		ll ans=1;
		for(int i=2;i<=m;i++) {
			ans=(1ll*v*(v-1)%mod*v%mod*(v-1)%mod*qpow(1ll*v*v,A[i].a-A[i-1].a-2)%mod
				)%mod*ans%mod;
		}
		ans*=qpow(1ll*v*v,A[1].a-1);
		ans%=mod;
		ans*=qpow(1ll*v*v,n-A[m].a);
		ans=(ans+qpow(v,A[m].a-A[1].a-m))%mod*qpow(1ll*v*v,A[1].a-1+n-A[m].a-1)%mod;
		cout<<(ans%mod);
	}
	cout<<endl;
}
int main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t; cin>>t;
	while(t--) solve();
}