#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAXN=1e5+5,MOD=1e9+7;
ll ksm(ll a,ll b=MOD-2) { ll s=1; for(;b;a=a*a%MOD,b>>=1) if(b&1) s=s*a%MOD; return s; }
int p[MAXN];
void solve() {
	int n,q,w;
	cin>>n>>q>>w;
	map <int,int> lim;
	bool flg=true;
	for(int x,y;q--;) {
		cin>>x>>y;
		if(lim.count(x)) flg&=(lim[x]==y);
		else lim[x]=y;
	}
	if(!flg) return cout<<"0\n",void();
	int m=0;
	for(auto it:lim) p[++m]=it.first;
	ll ans=ksm(w,2*(p[1]-1));
	for(int i=2;i<=m;++i) {
		ll wys=ksm(w,2*(p[i]-p[i-1]));
		wys=(wys-ksm(w,p[i]-p[i-1]-1)*(w-1))%MOD;
		ans=ans*(wys+MOD)%MOD;
	}
	ans=ans*ksm(w,2*(n-p[m]))%MOD;
	cout<<ans<<"\n";
}
signed main() {
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	int T; cin>>T;
	while(T--) solve();
	return 0;
}
