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

#define int long long
const int Maxn = 100005;
const int Mod = 1000000007;
int n, m, v, c[Maxn], d[Maxn], a[Maxn], b[Maxn], x[Maxn];

long long _20ans = 0; bool flag = 0;
void assi(int now) {
	if (flag) return ;
	if (now == n + 1) {
		bool isok = 1; 
		for (int i = 1; i <= n - 1; i ++)
			if (x[i] == a[i] && x[i + 1] != b[i]) {
				isok = 0;
				break;
			}
		if (!isok) return ;
		for (int i = 1; i <= m; i ++)
			if (x[c[i]] != d[i]) {
				isok = 0;
				break;
			}
		if (!isok) return ;
		flag = 1;
		return ;
	}
	for (int i = 1; i <= v; i ++) {
		if (flag) return ;
		x[now] = i, assi(now + 1);
	}
}
il bool check() {
	flag = 0, assi(1);
	return flag;
}
void dfs(int now) {
	if (now == n) {
		if (check()) _20ans ++, _20ans %= Mod;
		return ;
	}
	for (int i = 1; i <= v; i ++) {
		a[now] = i;
		for (int j = 1; j <= v; j ++)
			b[now] = j, dfs(now + 1);
	}
}
il void _20pts() {
	_20ans = 0, dfs(1);
	cout << _20ans << "\n";
}
il void solve20pts() {
	x[c[1]] = d[1];
	int res = 1;
	for (int i = 1; i <= n - 1; i ++) {
		if (i == c[m]) res *= (v + (v - 1) * v % Mod) % Mod, res %= Mod;
		else if (i + 1 == c[m]) res *= v, res %= Mod;
		res *= (v - 1) * v % Mod, res %= Mod;
	}
	cout << res << "\n";
	return ;
}
il void solve() {
	cin >> n >> m >> v;
	for (int i = 1; i <= m; i ++) cin >> c[i] >> d[i];
	if (n <= 12 && m <= 12 && v <= 2) { //1 ~ 5
		_20pts(); return ;
	}
	bool flagA = 1;
	for (int i = 1; i <= m; i ++)
		if (c[i] != i) {
			flagA = 0;
			break;
		}
	if (n == m && flagA) {
		int res = 1;
		for (int i = 1; i <= n - 1; i ++) {
			res = res * (((v - 1) * v  % Mod + 1) % Mod) % Mod;
		}
		cout << res << "\n";
		return ;
	}
	if (m == 1) { solve20pts(); return ; }
	int res = 1;
	for (int i = 1; i <= n - 1; i ++)
		res = res * v % Mod;
	cout << res << "\n";
}
signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T; cin >> T;
	while (T --) solve();
	return 0;
}

/*
3
2 1 2
1 1
2 2 2
1 1
2 2
2 2 2
1 1
1 2
*/

/*
10 11 2
10 2
7 2
7 2
2 2
3 2
4 2
10 2
7 2
10 2
3 2
3 2


12 2 2
7 2
7 2
*/
