#include<iostream>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+5,mod=1e9+7,M=1e5+1;
ll c[N],d[N];
ll fac[N],inv[N];
map<int,int>mp;
ll fpow(ll a,ll b){
	ll res=1ll;
	while(b>=1ll){
		if(b&1ll){
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b>>=1;
	}
	return res%mod;
}
void init(){
	fac[0]=1;
	for(int i=1;i<=M;i++){
		fac[i]=fac[i-1]*i%mod;
	}
	inv[M]=fpow(fac[M],mod-2);
	for(int i=M;i>=1;i--){
		inv[i-1]=inv[i]*i%mod;
	}
	return ;
}
ll C(ll n,ll m){
	ll p1=1,p2=1;
	for(ll i=1;i<=m;i++){
		p1=(p1*(n-i+1)%mod)%mod;
	}
	for(ll i=1;i<=m;i++){
		p2=(p2*i)%mod;
	}
	p2=fpow(p2,mod-2);
	return p1*p2%mod;
}
bool cmp(int a,int b){
	return a<b;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int t;
	init();
	cin>>t;
	while(t--){
		mp.clear();
		int n,m,v;
		bool flg=0;
		scanf("%d %d %d",&n,&m,&v);
		for(int i=1;i<=m;i++){
			scanf("%lld %lld",&c[i],&d[i]);
			if(mp.count(c[i])){
				if(mp[c[i]]!=d[i])flg=1;
			}
			mp[c[i]]=d[i];
		}
		if(flg)cout<<"0\n";
		else if(m==1)cout<<fpow(fpow(v,2),n-m)<<"\n";
		else if(n==m)cout<<fpow(v*(v-1)%mod+1,n-1)<<"\n";
		else{
			ll ans=1ll;
			sort(c+1,c+m+1,cmp);
			for(int i=1;i<=m-1;i++){
				ll res=c[i+1]-c[i]-1;
				ans=(ans*fpow(v,res))%mod;
			}
			cout<<ans<<"\n";
		}
	}
	return 0;
}
