// Code by Heratino & Nelofus
// No I still can't seem to
// Sing more than blues when It's time for my verse
// It's true that I need you
// To turn my blues into bursting fireworks
#include <bits/stdc++.h>
using i64 = long long;

constexpr int mod = 1e9 + 7;
inline int Add(const int &x, const int &y) {return x + y >= mod ? x + y - mod : x + y;}
inline int Sub(const int &x, const int &y) {return x - y < 0 ? x - y + mod : x - y;}
inline int Mul(const int &x, const int &y) {return 1ll * x * y % mod;}
inline void AddTo(int &x, const int &y) {x = Add(x, y);}
inline void SubTo(int &x, const int &y) {x = Sub(x, y);}
inline void MulTo(int &x, const int &y) {x = Mul(x, y);}
inline int fpow(int x, int k) {
#ifdef Insight
	std::cerr << x << ' ' << k << '\n';
#endif
	int res = 1;
	for (; k; k >>= 1, MulTo(x, x))
		if (k & 1)
			MulTo(res, x);
	return res;
}

inline void solve() {
	int n, m, v;
	std::cin >> n >> m >> v;
	std::vector<std::pair<int, int>> cond(m);
	for (auto &[c, d] : cond)	std::cin >> c >> d;
	std::sort(cond.begin(), cond.end());
	int v2 = Mul(v, v);
	int ans = fpow(v2, cond[0].first - 1);
	for (int i = 0; i < m; i++) {
		int j = i;
		while (j < m && cond[i].first == cond[j].first)
			j++;
		j--;
		if (cond[i].second != cond[j].second) {
			ans = 0;
			break;
		}
		if (i != 0) {
			int tmp = fpow(v2, cond[i].first - cond[i - 1].first);
			SubTo(tmp, Mul(v - 1, fpow(v, cond[i].first - cond[i - 1].first - 1)));
			MulTo(ans, tmp);
		}
		i = j;
	}
	MulTo(ans, fpow(v2, n - cond.back().first));
	std::cout << ans << '\n';
}

int main() {
#ifdef HeratinoNelofus
	freopen("input.txt", "r", stdin);
#else
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
#endif
	std::ios::sync_with_stdio(false);
	std::cin.tie(nullptr);

	int T;
	std::cin >> T;
	while (T--) {
		solve();
	}
	return 0;
}
