#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
constexpr LL mod = 1e9 + 7;
constexpr int maxm = 1e5;
LL n, m, v;
int c[maxm + 10], d[maxm + 10];
LL qpow(LL a, LL b) {
	a %= mod;
	LL res = 1;
	while (b) {
		if (b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}
map<int, int> mp;
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int T;
	cin >> T;
	bool flag = false;
	bool er = false;
	while (T--) {
		mp.clear();
		cin >> n >> m >> v;
		er = flag = false;
		for (int i = 1; i <= m; ++i) {
			cin >> c[i] >> d[i];
			if (mp[c[i]] && mp[c[i]] != d[i]) er = true;
			mp[c[i]] = d[i];
			if (c[i] != i) flag = true;
		}
		if (er) {
			cout << "0\n";
			continue;
		}
		cout << qpow((v * (v - 1) % mod + 1) % mod, n - 1) << '\n';
	}
	return 0;
}

