#include<bits/stdc++.h>
namespace B {
	const int M = 1e5 + 5;
	const long long Mod = 1e9 + 7;
	int t, n, m;
	long long v;
	struct Input {
		long long c, d;
	} input[M];
	template<typename T>
	inline T mod(const T value) {
		T result = value % Mod;
		if (result >= 0) {
			return result;
		} else {
			return result + Mod;
		}
	}
	template<typename T>
	T ksm(const T value, const int k) {
		if (k == 0) {
			return 1;
		}
		if (k == 1) {
			return value;
		}
		if (k & 1) {
			return mod(value * ksm(value, k - 1));
		} else {
			T result = ksm(value, k >> 1);
			return mod(result * result);
		}
	}
	inline void solve_A() {
		std::sort(input, input + m, [] (Input a, Input b) {
			return a.c < b.c;
		});
		const long long left = ksm(v, (input[0].c - 1) << 1), right = ksm(v, (n - input[m - 1].c) << 1);
//		std::printf("left %lld\n right %lld\n", left, right);
		long long ans = mod(left * right);
		for (int i = 0, pos = 1; i < m - 1; i++) {
			if (input[i + 1].c == input[i].c) {
				continue;
			}
//			std::printf("checkpoint C\n");
			ans = mod(ans * mod(ksm(v, (input[i + 1].c - input[i].c) << 1) - mod(ksm(v, input[i + 1].c - input[i].c - 1) * (v - 1))));
//			std::printf("checkpoint D\n");
		}
		std::cout << ans << "\n";
	}
	int main() {
		std::freopen("assign.in", "r", stdin);
		std::freopen("assign.out", "w", stdout);
		std::ios::sync_with_stdio(false);
		std::cin.tie(nullptr);
		std::cout.tie(nullptr);
		for (std::cin >> t; t; t--) {
			std::cin >> n >> m >> v;
			bool flag = false;
			{
				std::map<int, int> map;
				for (int i = 0; i < m; i++) {
					std::cin >> input[i].c >> input[i].d;
					auto it = map.find(input[i].c);
					if (it == map.end()) {
						map[input[i].c] = input[i].d;
					} else {
//						std::printf("checkpoint A\n");
						if (it->second != input[i].d) {
							flag = true;
						}
//						std::printf("checkpoint B\n");
					}
				}
			}
			if (flag) {
				std::cout << "0\n";
			} else {
				solve_A();
			}
		}
		return 0;
	}
}
int main()
{
	return B::main();
}
