#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN=1e5+5;
const int MOD=1e9+7;
int n,m,v;
ll ksm(ll a,ll b){ll r=1;while(b){if(b&1)r=r*a%MOD;a=a*a%MOD,b>>=1;}return r;}
struct node{int p,x;}a[MAXN];
void solve(){
	cin>>n>>m>>v;
	for(int i=1;i<=m;i++) cin>>a[i].p>>a[i].x;
	sort(a+1,a+m+1,[&](node x,node y){
		return x.p<y.p;
	});
	ll ans=ksm(v,2*(a[1].p-1+n-a[m].p));
	for(int i=1;i<m;i++){
		if(a[i].p==a[i+1].p) ans*=(a[i].x==a[i+1].x);
		else{
			int l=a[i+1].p-a[i].p; 
			ans=ans*(ksm(v,2*l)-ksm(v,l-1)*(v-1)%MOD)%MOD;
		} 
	}
	ans=(ans%MOD+MOD)%MOD;
	cout<<ans<<'\n';
}
int main(){
	ios::sync_with_stdio(false);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	int T;cin>>T;
	while(T--) solve();
}
// test small data
// compile in NOI Linux2

