#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pii = pair<int, int>;

const int N = 1e5 + 5;
const int mod = 1e9 + 7;

int qpow(int a, ll b) {
	int res = 1;
	while (b) {
		if (b & 1) res = 1ll * res * a % mod;
		a = 1ll * a * a % mod;
		b >>= 1;
	}
	return res;
}

int n, m, v;
int x[N];
struct Eq {
	int c, d;
	bool operator<(const Eq &b) const {return c < b.c;}
} a[N];
struct Constrains {
	int a, b;
} c[N];

int ans;
void dfs(int u) {
	if (u == n) {
		for (int i = 1; i <= n; i++) x[i] = 0;
		for (int i = 1; i <= m; i++)
			x[a[i].c] = a[i].d;
		for (int i = 1; i < n; i++)
			if (x[i] == c[i].a) {
				if (!x[i + 1]) x[i + 1] = c[i].b;
				else if (x[i + 1] != c[i].b) return;
			}
		ans = (ans + 1) % mod;
		return;
	}
	for (int i = 1; i <= v; i++)
		for (int j = 1; j <= v; j++) {
			c[u] = {i, j};
			dfs(u + 1);
		}
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int tt;
	cin >> tt;
	while (tt--) {
		cin >> n >> m >> v;
		for (int i = 1; i <= m; i++) cin >> a[i].c >> a[i].d;
		
		sort(a + 1, a + 1 + m);
		
		for (int i = 1; i < m; i++)
			if (a[i].c == a[i + 1].c && a[i].d != a[i + 1].d) {
				cout << "0\n";
				goto qwq;
			}
		
		if (m == 1) {
			cout << qpow(v, 2 * (n - 1)) << "\n";
		} else if (m == n) {
			cout << qpow((1ll * v * (v - 1) % mod + 1) % mod, n - 1) << "\n";
		} else if (v <= 2) {
			for (int i = 1; i <= n; i++) x[i] = 0;
			for (int i = 1; i <= m; i++)
				x[a[i].c] = a[i].d;
			ans = 0;
			dfs(1);
			cout << ans << "\n";
		} else {
			int cnt = 0;
			for (int i = 1; i < m; i++)
				if (a[i].c + 1 == a[i + 1].c)
					cnt++;
			cout << (qpow(v * v, 2ll * (n - cnt)) + cnt) % mod << "\n";
		}
		
		qwq:{}
	}
	return 0;
}
