#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MOD = 1e9+7,MAXN = 1e5+5;
struct node{int c,d;}nd[MAXN];
int t,n,m,v;

inline ll qpow(ll a,ll b){
	ll ans = 1;
	while(b){
		if(b & 1)
			ans = ans*a%MOD;
		a = a*a%MOD;
		b = b/2;
	}
	return ans; 
}

inline ll f(ll x){
	return qpow(v,x)*((qpow(v,x+2)-v+1+MOD)%MOD)%MOD;
}

void solve(){
	cin>>n>>m>>v;
	for(int i = 1;i<=m;i++)
		cin>>nd[i].c>>nd[i].d;
	sort(nd+1,nd+m+1,[&](node a,node b){return a.c<=b.c;});
	ll ans = qpow(v,2*nd[1].c-2);
	for(int i = 2;i<=m;i++){
		if(nd[i].c == nd[i-1].c && nd[i].d != nd[i-1].d){
			cout<<"0\n";
			return;
		}
		if(nd[i].c != nd[i-1].c)
			ans = ans*f(nd[i].c-nd[i-1].c-1)%MOD;
	}
	ans = ans*qpow(v,2*n-2*nd[m].c)%MOD;
	cout<<ans<<'\n';
}

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin>>t;
	while(t--)
		solve();
}

