#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define mp make_pair
#define A first
#define B second
#define pb push_back
using namespace std;
const int N=1e5+10;
const ll mod=1e9+7;
map<int,int>vis;
ll n,m,v,c[N],a[N],b[N],f[N][5];
ll qpow(ll x,ll y){
	ll ret=1;
	while(y){
		if(y&1)ret=ret*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return ret;
}
void solve(){
	vis.clear();
	cin>>n>>m>>v;
	bool p=1;
	for(int i=1,x;i<=m;i++){
		scanf("%lld%lld",&c[i],&x);
		if(vis[c[i]]&&vis[c[i]]!=x)p=0;
		vis[c[i]]=x;
	}
	if(!p){
		cout<<"0\n";
		return;
	}
	sort(c+1,c+m+1);
	m=unique(c+1,c+m+1)-c-2;
	for(int i=1;i<=m;i++){
		a[i]=(qpow(v,c[i+1]-c[i])-qpow(v,c[i+1]-c[i]-1)+mod)%mod;
		b[i]=(qpow(v,2*(c[i+1]-c[i]))-a[i]+mod)%mod;
	}
	ll ans=qpow(v,2*(c[1]-1))*qpow(v,2*(n-c[m+1]))%mod;
	for(int i=1;i<=m;i++)ans=ans*b[i]%mod;
	cout<<ans<<"\n";
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;
	while(T--)solve();
	return 0; 
}
