#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2e5+5;
ll t,n,m,v,tt,dp[2][N],mod=1e9+7,f,c[N];
map<int,int>mp;
ll qp(ll x,ll y){
	x%=mod;
	ll ct=1;
	while(y){
		if(y&1)ct=ct*x%mod;
		x=x*x%mod,y>>=1;
	}
	return ct;
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	scanf("%lld",&t);
	while(t--){
		scanf("%lld%lld%lld",&n,&m,&v);
		tt=0,f=1,mp.clear();
		for(int i=1,x,y;i<=m;i++){
			scanf("%d%d",&x,&y);
			if(mp[x]==0)mp[x]=y,c[++tt]=x;
			else if(mp[x]!=y)f=0;
		}
		if(f==0){
			cout<<"0\n";
			continue;
		}
		if(tt==0){
			cout<<qp(v,2*n-2)<<"\n";
			continue;
		}
		sort(c+1,c+1+tt);
		ll as=qp(v*v%mod,c[1]-1);
		for(int i=1;i<tt;i++){
			ll k=c[i+1]-c[i]-1;
			as=as*(qp(v,2*k+1)*(v-qp(qp(v,k),mod-2)+mod)%mod+qp(v,k))%mod;
		}
		if(c[tt]==n)cout<<as<<"\n";
		else{
			ll k=n-c[tt]-1;
			cout<<as*(qp(v,2*k+1)%mod*(v-qp(qp(v,k),mod-2)+mod)%mod+qp(v,n-c[tt]))%mod<<"\n";
		}
	}
	return 0;
}
