#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5, Mod = 1e9 + 7;
long long T, n, m, v;
int c[N], d[N]; map<int, int> a;
int qpow(int a, int k){
	if(k == 0) return 1;
	int t = qpow(a, k >> 1);
	return 1ll * t * t % Mod * (k & 1 ? a : 1) % Mod;
}
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin.tie(nullptr) -> sync_with_stdio(false);
	for(cin >> T; T --> 0;){
		cin >> n >> m >> v; a.clear();
		for(int i=1; i<=m; i++){
			cin >> c[i] >> d[i];
			if(a.count(c[i]) && a[c[i]] != d[i]){cout << 0; goto brk;}
			a[c[i]] = d[i];
		}
		if(a.size() == 1){cout << qpow(v, (n-1)*2) << "\n"; continue;}
		brk: cout << "\n";
	}
	return 0;
}
