#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5;
const int mod = 1e9 + 7;
int t;
int n, m, v;
int x[N + 5];
unordered_map<int, int> val;
int qpow(int a, int b) {
	if (b < 0) return 0;
	int ret = 1;
	for (; b; b >>= 1) {
		if (b & 1) ret = (LL)ret * a % mod;
		a = (LL)a * a % mod;
	}
	return ret;
}
void solve() {
	val.clear();
	scanf("%d%d%d", &n, &m, &v);
	bool flag = false;
	for (int i = 1; i <= m; i++) {
		int y;
		scanf("%d%d", &x[i], &y);
		if (!flag && val[x[i]] && val[x[i]] != y) {
			puts("0");
			flag = true;
		}
		val[x[i]] = y;
	}
	if (flag) return;
	sort(x + 1, x + m + 1);
	int ans = qpow(v, x[1] - 1 << 1);
	for (int i = 1; i < m; i++)
		ans = (LL)ans * ((qpow(v, x[i + 1] - x[i] << 1) - ((LL)v - 1) * qpow(v, x[i + 1] - x[i] - 1)) % mod + mod) % mod;
	ans = (LL)ans * qpow(v, n - x[m] << 1) % mod;
	printf("%d\n", ans);
}
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &t);
	while (t--) solve();
	return 0;
}
