#include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
const int MAX_M = 1e5 + 5, MOD = 1e9 + 7;

int t, n, m, v, ans, val[15];
int sz, c[MAX_M], a[15], b[15];
ll f[MAX_M][2];
pii l[MAX_M];

ll quick_power(ll a, ll b) {
	ll res = 1;
	for (; b; b >>= 1) {
		if (b & 1) res = res * a % MOD;
		a = a * a % MOD;
	}
	return res;
}

void dfs(int x) {
	if (x == n) {
		memset(val, 0, sizeof(val));
		for (int i = 1; i <= m; ++i) val[l[i].first] = l[i].second;
		for (int i = 1; i < n; ++i)
			if (val[i] == a[i]) {
				if (val[i + 1]) {
					if (val[i + 1] != b[i]) return;
				} else val[i + 1] = b[i];
			}
		++ans;
		return;
	}
	for (int i = 1; i <= v; ++i)
		for (int j = 1; j <= v; ++j) {
			a[x] = i;
			b[x] = j;
			dfs(x + 1);
		} 
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(nullptr);
	cin >> t;
	while (t--) {
		cin >> n >> m >> v;
		for (int i = 1; i <= m; ++i) cin >> l[i].first >> l[i].second;
		sort(l + 1, l + m + 1);
		m = unique(l + 1, l + m + 1) - (l + 1);
		ans = 0;
		bool suc = true;
		for (int i = 2; i <= m; ++i)
			if (l[i].first == l[i - 1].first && l[i].second != l[i - 1].second) {
				suc = false;
				break;
			}
		if (!suc) {
			cout << "0\n";
			continue;
		}
		for (int i = 1; i <= m; ++i) c[i] = l[i].first;
		if (v <= 2) {
			dfs(1);
			cout << ans << '\n';
		} else {
			f[1][0] = 0;
			f[1][1] = 1;
			for (int i = 2; i <= m; ++i) {
				f[i][0] = f[i - 1][1] * quick_power(v, c[i] - c[i - 1] - 1) % MOD * (v - 1) % MOD;
				f[i][1] = quick_power(v, 2ll * (c[i] - 1)) - f[i][0];
				f[i][1] = (f[i][1] % MOD + MOD) % MOD;
			}
			ll ans = quick_power(v, 2ll * (n - 1));
			for (int i = 1; i <= m; ++i) {
				ans -= quick_power(v, 2 * (n - c[i])) * f[i][0];
				ans = (ans % MOD + MOD) % MOD;
			}
			cout << ans << '\n';
		}
	}
	cout.flush();
	fclose(stdin);
	fclose(stdout);
	return 0;
}
