#include <bits/stdc++.h>
#define int long long
using namespace std;

const int mod = 1e9 + 7;

struct restrict_rule {
	int c, d;
} rules[100010];
int ans = 0;
int n, m, v;

int fastpow(int a, int b) {
	if(b == 0) {
		return 1;
	}
	int tmp = fastpow(a, b / 2);
	tmp *= tmp;
	tmp %= mod;
	if(b % 2) {
		tmp *= a;
		tmp %= mod;
	}
	return tmp;
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int t;
	scanf("%lld", &t);
	while(t--) {
		scanf("%lld %lld %lld", &n, &m, &v);
		for(int i = 1; i <= m; ++i) {
			scanf("%lld %lld", &rules[i].c, &rules[i].d);
		}
		sort(rules + 1, rules + 1 + m, [](const restrict_rule&a, const restrict_rule&b) {
			return a.c < b.c;
		});
		ans = fastpow(v, (rules[1].c - 1) * 2);
		int pre = rules[1].c;
		for(int i = 2; i <= m; ++i) {
			if(rules[i].c == rules[i - 1].c) {
				if(rules[i].d != rules[i - 1].d) {
					ans = 0;
					goto output_right;
				}
				continue;
			}
			if(rules[i].c == pre + 1) {
				ans *= (1) + (v) * (v - 1) % mod;
				ans %= mod;
			} else {
				int cnt = rules[i].c - pre - 1;
				ans *= (((fastpow(v, 2 * (cnt + 1)) - fastpow(v, cnt) * (v - 1) % mod + mod) + mod) % mod) % mod;
				ans %= mod;
			}
			pre = rules[i].c;
		}
		ans *= fastpow(v, (n - pre) * 2);
		ans %= mod;
output_right:
		printf("%lld\n", (int)ans);
	}
	return 0;
}
