#include<bits/stdc++.h>
#define int long long
using namespace std;

const int N = 2e5 + 10, mod = 1e9 + 7;

int qpow(int x, int y){
	x = x % mod; y = y % (mod - 1);
	int ret = 1;
	for(; y; y >>= 1, x = x * x % mod) if(y & 1ll) ret = ret * x % mod;
	return ret;
}

int n, m, V, c[N], siz[N], tot, L[N], R[N];
unordered_map<int, int> mp;

void solve(){
	memset(c, 0, sizeof c); memset(siz, 0, sizeof siz); memset(L, 0, sizeof L); memset(R, 0, sizeof R);
	mp.clear();
	cin >> n >> m >> V; tot = 0; bool fl = 0;
	for(int i = 1; i <= m; i++){
		int x; cin >> c[i] >> x;
		if(!mp.count(c[i])) mp[c[i]] = x;
		else if(mp[c[i]] != x) fl = 1;
	}
	if(fl == 1){
		cout << 0 << "\n";
		return;
	}
	sort(c + 1, c + m + 1); m = unique(c + 1, c + m + 1) - (c + 1); 
	int ans = 1;
	for(int i = 1; i <= m; i++){
		if(c[i] == c[i - 1] + 1 && i != 1) siz[tot]++;
		else siz[++tot] = 1, L[tot] = c[i], R[tot - 1] = c[i - 1];
	} R[tot] = c[m];
//	cout << tot << "\n";
//	for(int i = 1; i <= tot; i++) cout << L[i] << " " << R[i] << "\n";
//	cout << siz[1] << qpow((V * (V - 1) + 1) % mod, siz[1] - 1)<< "\n"; 
	ans = qpow(V * V % mod, L[1] - 1) * qpow((V * (V - 1) + 1) % mod, siz[1] - 1) % mod * qpow(V * V % mod, n - R[tot]) % mod;
//	cout << tot << "\n";
	for(int i = 2; i <= tot; i++){
		int len = L[i] - R[i - 1] - 1;
		int res = ((qpow(V * V % mod, len + 1) - qpow(V, len) * (V - 1) % mod) % mod + mod) % mod;
		ans = ans * res % mod * qpow((V * (V - 1) + 1) % mod, siz[i] - 1) % mod;
	} cout << ans << "\n";
}

signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
//	system("fc assign3.ans assign.out");
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int T; cin >> T; while(T--) solve();

	return 0;
}
/*
1
5 2 3
2 2
4 2
*/
