#include<bits/stdc++.h>
using namespace std;

#define int long long
const int N = 200000 + 10;
const int mod = 1e9 + 7;
int n, m, v;
int x[N], a[N], b[N];

int qp(int a, int b){
	int ans = 1;
	while(b){
		if(b & 1) ans = (ans * a) % mod;
		a = (a * a) % mod;
		b >>= 1;
	}
	return ans % mod;
}

void sola(){
	int c, d;
	cin >> c >> d;
	cout << qp(v % mod, 2 * (n - 1)) % mod << '\n';
}
int mul[N];
int inv(int p){return qp(p, mod - 2);}
void solb(){
	mul[0] = 1;
	for(int i = 1 ; i <= n ; i ++)
		mul[i] = mul[i - 1] * i % mod;
	int ans = 0;
	n --;
	for(int i = 0 ; i <= n ; i ++){
		int t = (mul[n] * inv(mul[i]) % mod * inv(mul[n - i]) % mod) % mod;
		ans = (ans + qp((v - 1) * v % mod, (n - i)) % mod * t % mod) % mod;
//		cout << ans << '\n';
	}
	cout << ans << '\n';
}
void solve(){
	cin >> n >> m >> v;
	if(m == 1){
		sola();
		return;
	}
	bool flg = 0;
	for(int i = 1 ; i <= n ; i ++){
		int c, d;
		cin >> c >> d;
		if(x[c]) flg = 1;
		x[c] = d;
	}
	if(flg){
		cout << 0 << '\n';
		return;
	}
	if(m == n){
		solb();
		return;
	}
}

signed main(){
	 freopen("assign.in", "r", stdin);
	 freopen("assign.out", "w", stdout);
	int t;
	cin >> t;
	while(t --)
		solve();
}
