#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod=1e9+7;
ll T,n,m,v,ans,c[100005],d[100005],p,l,t,cnt,tc[100005];
set<int> s;
map<int,int> mp;
ll fast_mi(ll a,ll b){
	if(b==0) return 1;
	if(b==1) return a;
	ll mid=fast_mi(a,b/2);
	if(b&1) return (((mid*mid)%mod)*a)%mod;
	else return (mid*mid)%mod;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld",&n,&m,&v),p=n-1,ans=1,s.clear(),mp.clear(),cnt=0;
		for(int i=1;i<=m;i++){
			scanf("%lld%lld",&c[i],&d[i]);
			if(mp.find(c[i])==mp.end()) mp[c[i]]=d[i],tc[++cnt]=c[i];
			else{
				if(mp[c[i]]==d[i]) continue;
				else ans=0;
			}
			s.insert(c[i]);
		}
		if(ans==0){
			printf("0\n");
			continue;
		}
		sort(tc+1,tc+cnt+1);
		for(int i=1;i<cnt;i++){
			if(s.upper_bound(tc[i])==s.end()) continue;
			l=*s.upper_bound(tc[i]);
			t=(l-tc[i]-1),p-=(l-tc[i]);
			ans=(ans*((fast_mi((v*v)%mod,(l-tc[i]))%mod-((fast_mi(v,t)*(v-1))%mod)+mod)%mod))%mod;
		}
		ans=(ans*fast_mi((v*v)%mod,p))%mod;
		printf("%lld\n",ans);
	}
	return 0;
}
/*
神奇，第一题做不出来，结果第二题样例全过了
先去打个暴力再回来乱搞

没时间了，呜呜呜 
*/
