#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll rwt = 1;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
struct Node {
	int Xi, Ki;
} s0[N];
int s1[N], t, n, n1, m, q, ans;
void Solve();
bool cmp1(Node x, Node y);
int qpow(int x, int k);
void smd(int &x, int y);
void dmd(int &x, int y);
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &t);
	while (t--) {
		Solve();
	}
	return 0;
}
void Solve() {
	int T1, T2, T3 = 0;
	ans = 0;
	scanf("%d%d%d", &n, &q, &m);
	for (int i = 1; i <= q; i++) {
		scanf("%d%d", &s0[i].Xi, &s0[i].Ki);
	}
	sort(s0 + 1, s0 + q + 1, cmp1);
	for (int i = 1; i < q; i++) {
		if (s0[i].Xi == s0[i + 1].Xi && s0[i].Ki != s0[i + 1].Ki) {
			printf("0\n");
			return;
		}
	}
	s1[n1 = 1] = s0[1].Xi;
	for (int i = 2; i <= q; i++) {
		if (s0[i].Xi != s0[i - 1].Xi) {
			s1[++n1] = s0[i].Xi;
		}
	}
	T1 = qpow(m, s1[1] + s1[1] - 2);
	for (int i = 2; i <= n1; i++) {
		T2 = rwt * T1 * qpow(m, s1[i] - s1[i - 1] - 1) % mod;
		T3 = rwt * T3 * qpow(m, s1[i] + s1[i] - s1[i - 1] - s1[i - 1]) % mod;
		smd(ans, rwt * T2 * (m - 1) % mod * qpow(m, n + n - s1[i] - s1[i]) % mod);
		smd(T3, rwt * T2 * (m - 1) % mod);
		T1 = qpow(m, s1[i] + s1[i] - 2);
		dmd(T1, T3);
	}
	T1 = qpow(m, n + n - 2);
	dmd(T1, ans);
	printf("%d\n", T1);
	return;
}
bool cmp1(Node x, Node y) {
	return x.Xi < y.Xi;
}
int qpow(int x, int k) {
	if (k < 1) {
		return 1;
	}
	int Ans = 1;
	for (int i = k; i; i >>= 1, x = rwt * x * x % mod) {
		if (i & 1) {
			Ans = rwt * Ans * x % mod;
		}
	}
	return Ans;
}
void smd(int &x, int y) {
	x = ((x + y < mod) ? x + y : x + y - mod);
	return;
}
void dmd(int &x, int y) {
	x = ((x < y) ? x - y + mod : x - y);
}
