#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
const int N = 1e5 + 5;
const ll P = 1e9 + 7;
int T, n, m, c[N], d[N], x[N];
ll v;
ll qpow(ll x, int y){
	ll res = 1;
	for(; y; y >>= 1, x = x * x % P)
		if(y & 1) res = res * x % P;
	return res;
}
signed main(){
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	cin >> T;
	while(T--){
		cin >> n >> m >> v;
		memset(x, 0, sizeof(int) * (n + 1));
		int flg = 0;
		for(int i = 1; i <= m; ++i){
			cin >> c[i] >> d[i];
			if(!x[c[i]]) x[c[i]	] = d[i];
			else if(x[c[i]] != d[i]){
				flg = 1;
				cout << 0 << endl;
			}
		}
		if(flg) continue;
		cout << qpow(v, n - m) << endl;
	}
	return 0;
}
/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/
