#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll mod=1e9+7,N=1e5+1e4;
int T;
ll n,m,v;
struct node{
	ll p,v;
};
bool cmp(node a,node b){
	return a.p<b.p;
}
ll qp(ll a,ll b){
	if(b<0) return 1;
	if(b==0) return 1;
	if(b==1) return a%mod;
	if((b&1)) return qp(a,b-1)%mod*a%mod;
	else{
		ll ret=qp(a,b/2)%mod;
		return ret*ret%mod;
	}
}
node g[N];
void solve(){
	cin>>n>>m>>v;
	ll ans=qp(v,(n-1)*2);
	for(int i=1;i<=m;i++){
		cin>>g[i].p>>g[i].v;
	}
	sort(g+1,g+1+m,cmp); 
	g[0].p=0;
	ll tp=1;
	ll sum=1;
	ll l=0;
	ll now=0;
	for(int i=2;i<=m;i++){
		if(g[i].p==g[i-1].p && i!=1 && g[i].v!=g[i-1].v){
			cout<<0<<endl;
			return;
		}
		qp(v,(n-g[i].p-1)*2);
		tp=(qp(v,g[i].p-g[i-1].p-1)%mod*(v-1)%mod+mod)%mod;
		now=sum*tp%mod;
		ans-=now*qp(v,(n-g[i].p)*2)%mod;
		ans=(ans+mod)%mod;
		sum=(sum*(qp(v,(g[i].p-g[i-1].p)*2)%mod-tp)+mod)%mod;
		sum%=mod;
	}
	cout<<ans<<endl;
	return;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>T;
	for(int o=1;o<=T;o++){
		solve();
	}
	return 0;
}
