#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define MAXN 100005
long long qpow(long long n,long long m){
	long long r=1;
	for(;m;m>>=1,n=n*n%MOD)if(m&1)r=r*n%MOD;
	return r;
}
int n,m,v;
long long ans=1;
vector<pair<int,int> >vc;
void solve(){
	cin>>n>>m>>v;
	vc.clear();
	vc.resize(m);
	for(auto&it:vc)cin>>it.first>>it.second;
	sort(vc.begin(),vc.end());
	for(int i=0;i+1<(int)vc.size();++i){
		if(vc[i].first==vc[i+1].first&&vc[i].second!=vc[i+1].second){
			cout<<"0\n";
			return;
		}
	}
	ans=1;
	for(int i=0;i+1<(int)vc.size();++i){
		if(vc[i].first<vc[i+1].first){
			int l=vc[i+1].first-vc[i].first-1;
//			cerr<<l<<'\n';
			long long w=(qpow(v,2*(l+1))-qpow(v,l)*(v-1)%MOD+MOD)%MOD;
			ans=ans*w%MOD;
		}
	}
//	cout<<ans<<'\n';
	ans=ans*qpow(v,2*(vc[0].first-1))%MOD;
	ans=ans*qpow(v,2*(n-vc.back().first))%MOD;
	cout<<ans<<'\n';
}
signed main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int t;cin>>t;while(t--)solve();
} 
