#include <bits/stdc++.h>
#define MAXN 100005
using ll = long long;
using namespace std;
const int mod = 1e9+7;
//Thus the grace I had will always in my heart.
struct node{
	int pos,v;
	bool operator<(const node &b)const{
		return pos==b.pos ? v<b.v : pos<b.pos;
	}
	bool operator==(const node &b)const{
		return pos==b.pos && v==b.v;
	}
}a[MAXN];
int n,m,v;
ll qpow(ll x,ll k){
	ll ret=1;
	while(k){
		if(k&1)ret=ret*x%mod;
		x=x*x%mod;k>>=1; 
	}
	return ret;
}
void solve(){
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++){
		cin>>a[i].pos>>a[i].v;
	} 
	sort(a+1,a+m+1);
	m=unique(a+1,a+m+1)-a-1;
	for(int i=1;i<m;i++){
		if(a[i].pos==a[i+1].pos && a[i].v!=a[i+1].v){
			cout<<0<<"\n";
			return;
		}
	}
	ll ans = 1;
	for(int i=2;i<=m;i++){
		int dis = a[i].pos - a[i-1].pos;
		 
		ll f = qpow(v,dis*2)-qpow(v,dis-1)*(v-1)%mod;
		f=(f%mod+mod)%mod;
		ans=ans*f%mod;
	
	}
	//pre
	ans=ans*(qpow(v,2ll*(a[1].pos-1)))%mod;
	//suf
	ans=ans*(qpow(v,2ll*(n-a[m].pos)))%mod;
	cout<<ans<<"\n"; 
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin.tie(nullptr)->sync_with_stdio(false);
	int T;cin>>T;while(T--)solve(); 
	return 0;
} 
