#include <bits/stdc++.h>

const int MOD = 1e9 + 7;

int t, n, m;
long long v;
std::unordered_map<int, int> st;

namespace solve1 {
	const int N = 12 + 1;
	int a[N], b[N], arr[N];
	long long ans = 0;
	inline bool check() {
		for (int i = 1; i <= n; i++) {
			if (i > 1 && a[i - 1] == arr[i - 1])
				arr[i] = b[i - 1];
			else
				arr[i] = st[i] ? st[i] : 3 - a[i];
			if (st[i] && st[i] != arr[i])
				return false;
		}
		return true;
	}
	void dfs(int x) {
		if (x >= n) {
			if (check())
				ans++;
			return;
		}
		a[x] = 1, b[x] = 1, dfs(x + 1);
		a[x] = 1, b[x] = 2, dfs(x + 1);
		a[x] = 2, b[x] = 1, dfs(x + 1);
		a[x] = 2, b[x] = 2, dfs(x + 1);
	}
	inline long long solve() {
		ans = 0, dfs(1);
		return ans;
	}
};
long long modpow(long long a, long long b) {
	long long res = 1;
	a %= MOD;
	while (b) {
		if (b & 1)
			res = (res * a) % MOD;
		a = (a * a) % MOD;
		b >>= 1;
	}
	return res;
}

bool flag;
long long solve() {
	scanf("%d%d%lld", &n, &m, &v);
	st.clear(), flag = false;
	for (int i = 1, c, d; i <= m; i++) {
		scanf("%d%d", &c, &d);
		if (st[c] && st[c] != d)
			flag = true;
		st[c] = d;
	}
	if (flag)
		return 0;
	if (v == 1)
		return 1;
	if (n <= 12 && v == 2)
		return solve1::solve();
	if (st.size() == m && n == m)
		return modpow(v * (v - 1) + 1, n - 1);
	if (m == 1)
		return modpow(v * v, n - 1);
	return 0;
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	scanf("%d", &t);
	while (t--)
		printf("%lld\n", solve());
	return 0;
}
