#include<bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
using namespace std;
typedef long long ll;
const int N = 300005,mod = 1e9 + 7;
map<ll,ll> vis;
ll n,m,v,a[N],b[N];
ll Pow(ll x,ll y){
	ll res = 1;
	while(y){
		if(y & 1)res = res * x % mod;
		x = x * x % mod,y >>= 1;
	}
	return res;
}
void solve(){
	cin >> n >> m >> v;
	for(int i = 1;i <= m;i++)
		cin >> a[i] >> b[i];
	vis.clear();
	for(int i = 1;i <= m;i++){
		if(vis[a[i]] != 0 && vis[a[i]] != b[i])
			return cout << "0\n",void();
		vis[a[i]] = b[i];
	}
	ll ans = Pow(v * v % mod,(*vis.begin()).fi - 1 + n - (*--vis.end()).fi);
	for(auto it = ++vis.begin();it != vis.end();it++){
		ll nn = (*it).fi - (*prev(it)).fi;
		ans = ans * ((Pow(v * v % mod,nn) - Pow(v,nn - 1) * (v - 1)) % mod + mod) % mod;
	}
	cout << ans << '\n';
}
int main(){
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin.tie(0);
	ios::sync_with_stdio(0);
	int T;cin >> T;
	while(T--)solve();
	return 0;
}

