#include<bits/stdc++.h>
#define ll long long
#define pii array<ll,2> 
using namespace std;
const ll N=2e5+5,md=1e9+7;
ll T,n,m,v;
pii a[N];
ll mi(ll a,ll b){
	a%=md;
	ll c=a,d=1;
	while(b){
		if(b&1)d=d*c%md;
		c=c*c%md;
		b>>=1;
	}
	return d;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld %lld %lld",&n,&m,&v);
		for(ll i=1;i<=m;i++){
			scanf("%lld %lld",&a[i][0],&a[i][1]);
		}
		sort(a+1,a+1+m);
		ll ans=1,ct1=0,ct2=0;
		for(ll i=1;i<m;i++){
			ll la=a[i][0],ra=a[i][1],lb=a[i+1][0],rb=a[i+1][1];
			if(la==lb){
				if(ra!=rb)ans*=0;
			}
			else if(la+1==lb){
				ct1++;
			}
			else{
				ct2++;
			}
		}
		ans=ans*mi(v*v,n-1-ct2-ct1)%md;
		ans=ans*mi(v,ct2)%md;
		ans=ans*mi(v*v-v+1,ct1)%md;
		printf("%lld\n",ans);
	}
	return 0;
}
