#include <bits/stdc++.h>
#define fst first
#define snd second
using namespace std;
using ll = long long;
using ld = long double;

const long long IMX = 1ll << 30;
const long long LMX = 1ll << 60;
const long long MOD = 1000000007;
ll T, n, m, v;
ll qpow(ll n, ll m, ll p) {
	ll res = 1;
	while (m) {
		if (m & 1) res = res % p * n % p;
		n = n % p * n % p, m >>= 1;
	}
	return res;
}
int main() {
	ios :: sync_with_stdio(0);
	cin.tie(nullptr);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> T;
	while (T--) {
		map <int, int> mp;
		bool flag = 1;
		cin >> n >> m >> v;
		for (int i = 1; i <= m; i++) {
			int x, y;
			cin >> x >> y;
			if (mp[x] and mp[x] != y) flag = 0;
			else mp[x] = y;
		}
		if (!flag) {
			cout << "0\n";
			continue;
		}
		if (n == m) {
			cout << qpow((v * v - v + 1) % MOD, n - 1, MOD) << '\n';
			continue;
		}
		cout << "-1\n";
	}
	return 0;
}

