#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define int long long
const int N = 100010, mod = 1e9 + 7;
long long cj(int a, int b) {
	return (a * b) % mod;
}
long long ksm(int a, int b) {
	if (b == 0) return 1ll;
	if (b == 1) return a % mod;
	long long ans = ksm(a, b / 2) % mod;
	if (b % 2 == 0) return cj(ans, ans);
	else return cj(cj(ans, ans), a);
}
int T, n, m, v, p[N], idx = 0;
struct lim {
	int c, d;
	bool operator< (const lim &l) const {
		return c < l.c;
	}
}x[N];
signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	cin >> T;
	while (T --) {
		idx = 0;
		scanf("%lld %lld %lld", &n, &m, &v);
		v = v % mod;
		for (int i = 1; i <= m; ++ i) p[i] = 0;
		if (m == 0) {
			printf("%lld\n", ksm(v, 2 * n));
			continue;
		}
		for (int i = 1; i <= m; ++ i) scanf("%lld %lld", &x[i].c, &x[i].d);
		sort(x + 1, x + m + 1);
		
		bool al = false;
		for (int i = 1; i <= m;) {
			int j = i + 1;
			while (j <= m && x[j].c == x[i].c) {
				if (x[j].d != x[i].d) {
					al = true;
					break;
				}
				j ++;
			}
			if (al) break;
			p[++ idx] = x[i].c;
			i = j;
		}
		if (al) {
			printf("0\n");
			continue;
		}
		int ans = ksm(v, 2 * (n - 1 - (p[idx] - p[1])));
		for (int i = 1; i < idx; ++ i) {
			int l = ksm(v, 2 * (p[i + 1] - p[i]));
			l -= ksm(v, p[i + 1] - p[i]) - ksm(v, p[i + 1] - p[i] - 1);
			ans = (l * ans) % mod;
		}
		printf("%lld\n", ans);
	}
	return 0;
}
