#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1000000007;

LL qpow(LL x, LL cnt) {
	LL ret = 1, bas = x, c = cnt;
	while (c) {
		if (c & 1) ret = ret * bas % mod;
		bas = bas * bas % mod;
		c >>= 1;
	}
	return ret;
}

int T, n, m, v, lsh[100005], lm[100005];
struct lim { int x, w; } e[100005];

void work() {
	scanf("%d %d %d", &n, &m, &v);
	for (int i = 1; i <= m; i++) lsh[i] = lm[i] = 0;
	for (int i = 1; i <= m; i++) e[i].x = e[i].w = 0;
	for (int i = 1; i <= m; i++) {
		scanf("%d %d", &e[i].x, &e[i].w);
		lsh[i] = e[i].x;
	}
	sort(lsh + 1, lsh + m + 1);
	int len = unique(lsh + 1, lsh + m + 1) - lsh - 1;
	for (int i = 1; i <= m; i++) {
		e[i].x = lower_bound(lsh + 1, lsh + len + 1, e[i].x) - lsh;
		if (lm[e[i].x] && lm[e[i].x] != e[i].w) return printf("0\n"), void();
		lm[e[i].x] = e[i].w;
	}
	LL ans = 1;
	for (int i = 1; i < len; i++) {
		ans = ans * (qpow(v, 2ll * (lsh[i + 1] - lsh[i])) - qpow(v, lsh[i + 1] - lsh[i] - 1) * (v - 1) % mod + mod) % mod;
	}
	ans = ans * qpow(v, 2ll * (lsh[1] - 1) + 2ll * (n - lsh[len])) % mod;
	printf("%lld\n", ans);
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &T);
	while (T--) work();
	return 0;
}
