#include<bits/stdc++.h>
using namespace std;
using ui=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
using u128=__uint128_t;
using pii=pair<int,int>;
#define fi first
#define se second
constexpr int N=1e5+5,mod=1e9+7;
int n,m,V,val[N];pii e[N];
inline ll qpow(ll a,ll b){
	ll res=1;
	for(;b;b>>=1,a=a*a%mod)
		if(b&1)res=res*a%mod;
	return res;
}
void solve(){
	cin>>n>>m>>V;
	for(int i=1;i<=m;i++)
		cin>>e[i].fi>>e[i].se;
	sort(e+1,e+m+1);
	for(int i=2;i<=m;i++)
		if(e[i].fi==e[i-1].fi&&e[i].se!=e[i-1].se){
			cout<<0<<'\n';
			return;
		}
	ll ans=1;
	ans=ans*(qpow(V,(e[1].fi-1)<<1))%mod;
	ans=ans*(qpow(V,(n-e[m].fi)<<1))%mod;
	for(int i=2;i<=m;i++){
		int r=e[i].fi,l=e[i-1].fi;
		if(l==r)continue;
		ans=ans*(qpow(V,(r-l)<<1)-qpow(V,r-l-1)*(V-1)%mod+mod)%mod;
	}
	cout<<ans<<'\n';
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int _Test;cin>>_Test;
	while(_Test--)solve();
	return 0;
}

