#include <bits/stdc++.h>

#ifdef XSLOCAL
//#define XS_DEBUG_MODE
#endif

#ifndef XS_DEBUG_MODE
#define dblog(str, args...)
#else
#define dblog(str, args...) (fprintf(stderr, str, ##args))
#endif

namespace xiaosuan {

using std::pair;
using std::sort;

using sll = long long;
using lmt_T = pair <int, int>;

const int MAX_M = int(1e5);
const int MOD = int(1e9) + 7;

#define sfop(x, y, op) ((sll(x) op sll(y)) % MOD)
#define sfad(x, y) sfop(x, y, +)
#define sfsb(x, y) sfad(x, MOD - y)
#define sfml(x, y) sfop(x, y, *)

int qpow(int a, int b) {
	int c = 1;
	while (b) {
		if (b & 1) c = sfml(c, a);
		b >>= 1;
		a = sfml(a, a);
	}
	return c;
}

int n, m, v;

lmt_T A[MAX_M + 10];

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 0; i < m; ++i) {
			scanf("%d%d", &A[i].first, &A[i].second);
		}
		sort(A, A + m, [](lmt_T a, lmt_T b) {
			return a.first < b.first;
		});
		int ans = qpow(v, 2 * (A[0].first - 1 + n - A[m - 1].first));
		dblog("%d\n", ans);
		for (int i = 1; i < m; ++i) {
			int sz = A[i].first - A[i - 1].first;
			if (sz == 0) {
				if (A[i].second != A[i - 1].second) {
					ans = 0;
					break;
				}
			} else {
				int tmp = sfsb(qpow(v, 2 * sz), sfml(qpow(v, sz - 1), v - 1));
				dblog("%d %d %lld\n", sz, qpow(v, 2 * sz), sfml(qpow(v, sz - 1), v - 1));
				dblog("%d %d\n", sz, tmp);
				ans = sfml(ans, tmp);
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

}

int main() {
	return xiaosuan::main();
}
