#include <iostream>
#include <vector>
#include <unordered_map>
#define MOD 1000000007
using namespace std;
long long quick_pow(long long x, long long y, long long mod) {
long long res = 1;
while (y > 0) {
if (y & 1) res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
void solve() {
int T;
cin >> T;
while (T--) {
int n, m, v;
cin >> n >> m >> v;
unordered_map<int, int> fixed_values;
bool conflict = false;
for (int i = 0; i < m; i++) {
int c, d;
cin >> c >> d;
if (fixed_values.count(c) && fixed_values[c] != d) {
conflict = true;
}
fixed_values[c] = d;
}
if (conflict) {
cout << 0 << endl;
continue;
}
long long valid_pairs = 0;
for (int a = 1; a <= v; a++) {
for (int b = 1; b <= v; b++) {
bool is_valid = true;
for (auto [pos, value] : fixed_values) {
if ((pos == 1 && a != value) || (pos == n && b != value)) {
is_valid = false;
break;
}
}
if (is_valid) valid_pairs++;
}
}
valid_pairs %= MOD;
long long result = quick_pow(valid_pairs, n - 1, MOD);
cout << result << endl;
}
}
int main() {
solve();
return 0;
}