#include <bits/stdc++.h>
using namespace std;
const long long maxn=1e5+5, mod=1e9+7;
long long T, n, m, c[maxn], d[maxn],v;
void solve() {
	long long ans=1, myt=2*n-2;
	for (long long i = 1; i <= myt; i++) ans*=v,ans%=mod;
	cout << ans%mod << endl;
}
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	cin >> T;
	while (T--) {
		cin >> n >> m >> v;
		bool flag=true;
		for (long long i = 1; i <= m; i++) cin >> c[i] >> d[i];
		for (long long i = 1; i <= m; i++) {
			for (long long j = 1; j <= m; j++) {
				if(c[i]==c[j]&&d[i]!=d[j]) {
					cout << 0 << endl;
					flag=false;
					break;
				} 
			}
			if (!flag) break;
		}
		if (!flag) continue;
		solve();
	}
	return 0;
} 
