#include <bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e5 + 10, P = 1e9 + 7;

int n, m, v;
pair<int, int> lim[N];
int cnt;

int Pow(int a, int b) {
	int res = 1;
	for (; b; b >>= 1, a = a * a % P)
		if (b & 1) res = res * a % P;
	return res;
}

void solve() {
	cin >> n >> m >> v;
	for (int i = 1; i <= m; i++) {
		cin >> lim[i].first >> lim[i].second;
	}
	sort(lim + 1, lim + m + 1);
	
	cnt = 0;
	for (int i = 1; i <= m; i++) {
		if (lim[cnt].first != lim[i].first) {
			lim[++cnt] = lim[i];
		} else if (lim[cnt].second != lim[i].second) {
			cout << "0\n";
			return;
	  }
	}
//	for (int i = 1; i <= cnt; i++) {
//		printf("%lld: %lld %lld\n", i, lim[i].first, lim[i].second);
//  }
	int ans = Pow(v, 2 * (lim[1].first - 1));
	for (int i = 2; i <= cnt; i++) {
		int l = lim[i].first - lim[i - 1].first;
//		printf("l:%lld\n", l);
		ans = ans * (Pow(v, 2 * l) - (v - 1) * Pow(v, l - 1) % P + P) % P;
	}
	ans = ans * Pow(v, 2 * (n - lim[cnt].first)) % P;
	cout << ans << "\n";
}

signed main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	int t;
	cin >> t;
	while (t--) solve();
	
	return 0;
}
