#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 10;
int n, m, vi;
int c[N], d[N];

int read() {
	int s = 0, w = 1;
	char ch = getchar();
	for ( ; !isdigit(ch); ch = getchar())	if (ch == '-')	w = -1;
	for ( ; isdigit(ch); ch = getchar())	s = s * 10 + ch - '0';
	return s * w;
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T = read();
	int cnt = 1;
	while (T--) {
		n = read(), m = read(), vi = read();
		for (int i = 1; i <= m; i++) {
			c[i] = read(), d[i] = read();
		}
			vector<int> v(n + 10, 0);
			bool flag = false;
			for (int i = 1; i <= m; i++) {
				if (v[c[i]] != 0 && v[c[i]] != d[i]) {
					flag = true;
					break;
				}
				v[c[i]] = d[i];
			} 
			if (flag) {
				printf("0\n");
				continue;
			}
			int res = 1;
			for (int i = 1; i < n; i++) {
				int ans = 0;
				for (int a = 1; a <= vi; a++) {
					for (int b = 1; b <= vi; b++) {
						if (v[i] == a) {
							if (v[i + 1] == b || v[i + 1] == 0)	ans++;
						}
						else {
							ans++;
						}
					}
				}
				res *= ans;
				res %= mod;
			}
			printf("%lld\n", res % mod);
	}
	
	return 0;
}
