#include <cstdio>
#include <algorithm>

namespace Main {

using namespace std;

typedef long long LL;

LL read() {
	LL res, f = 1, c;
	for (c = getchar(); (c < '0' || c > '9') && c != '-';)
		c = getchar();
	if (c == '-') {
		f = -1;
		c = getchar();
	}
	for (res = 0; '0' <= c && c <= '9'; c = getchar())
		res = res * 10 + (c & 15);
	return res * f;
}

constexpr bool T = 1;
constexpr int N = 100005, MOD = 1e9 + 7;

LL pow(LL x, LL y = MOD - 2, LL m = MOD) {
	LL res = 1;
	for (x %= m; y; y >>= 1) {
		if (y & 1)
			res = res * x % m;
		x = x * x % m;
	}
	return res;
}

pair<int, int> a[N];

void solve() {
	int n = read(), m = read(), v = read();
	for (int i = 1; i <= m; ++i) {
		a[i].first = read();
		a[i].second = read();
	}
	sort(a + 1, a + 1 + m);
	LL ans = pow(v, 2 * (a[1].first - 1)) * pow(v, 2 * (n - a[m].first)) % MOD;
	for (int i = 1; i < m; ++i) {
		int c = a[i + 1].first - a[i].first;
		if (c)
			ans = ans * (pow(v, 2 * c) - pow(v, c - 1) * (v - 1) % MOD) % MOD;
		else if (a[i + 1].second != a[i].second) {
			puts("0");
			return;
		}
	}
	printf("%lld\n", ans < 0 ? ans + MOD : ans);
	return;
}

void main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	for (int t = T ? read() : 1; t; --t)
		solve();
	return;
}
}

int main() {
	Main::main();
	return 0;
}

