#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10, mod = 1e9 + 7;
inline int qpow(int a, int b) {
	int ans = 1;
	while (b) {
		if (b & 1)
			(ans *= a) %= mod;
		(a *= a) %= mod;
		b >>= 1;
	}
	return ans;
}
map<int, int> ds;
int a[N];
int cnt;
int n, m, v;
inline int sol() {
	cnt = 0;
	ds.clear();
	cin >> n >> m >> v;
	for (int i = 1, x, y; i <= m; i++) {
		cin >> x >> y;
		int& tmp = ds[x];
		if (!tmp || tmp == y)
			tmp = y;
		else
			return 0;
	}
	for (const pair<int, int>& i : ds)
		a[++cnt] = i.first;
	int ans = 0;
	for (int i = 1; i <= cnt; i++) {
		for (int j = i + 1; j <= cnt; j++) {
			(ans += qpow(v, a[j] - a[i] - j + i) % mod) %= mod;
		}
	}
	return (mod + qpow(v, n - 1 << 1) - ans * (v - 1) % mod * qpow(v, a[1] - 1 + n - a[cnt] << 1) % mod) % mod;
}

signed main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while (T--)
		cout << sol() << '\n';
}