#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
#define MP make_pair
using namespace std;

const int N = 1e5 + 7, MOD = 1e9 + 7;

int fpow(int x, int y) {
	int ans = 1;
	for (; y; x = x * x  % MOD, y >>= 1)
		if (y & 1) ans = ans * x % MOD;
	return ans;
}

map<int, int> mp;
int n, m, v, c, pos[N], ans;

void solve() {
	cin >> n >> m >> v;
	bool flg = true;
	mp.clear(), c = 0, ans = 1;
	for (int i = 1, x, y; i <= m; i ++) {
		cin >> x >> y;
		if (mp.find(x) != mp.end() && mp[x] != y) flg = false;
		mp[x] = y;
	}
	if (!flg) return cout << "0\n", void();
	for (auto i : mp) pos[++ c] = i.fi; 
	for (int i = 1; i < c; i ++) {
		int num = fpow(v * v % MOD, pos[i + 1] - pos[i]);
		num = (num + MOD - fpow(v, pos[i + 1] - pos[i] - 1) * (v - 1) % MOD) % MOD;
		ans = ans * num % MOD;
	}
	ans = ans * fpow(v * v % MOD, pos[1] - 1) % MOD;
	ans = ans * fpow(v * v % MOD, n - pos[c]) % MOD;
	cout << ans << '\n';
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0); 
	int tt; cin >> tt;
	while (tt --) solve();
	return 0;
}
 
