#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
typedef long long ll;
constexpr int N = 1e5;
constexpr ll mod = 1e9 + 7;
ll powx(ll a, ll p) {
	ll res = 1;
	while(p) {
		if(p & 1ll) res = res * a % mod;
		a = a * a % mod, p >>= 1;
	}
	return res;
}
int T, n, m, v;
ll ans;
struct ciallo {
	int c, d;
	bool operator < (const ciallo &_ciallo) const {
		return c < _ciallo.c;
	}
} a[N + 3];
int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &T);
	while(T--) {
		scanf("%d %d %d", &n, &m, &v);
		for(int i = 1; i <= m; i++) {
			scanf("%d %d", &a[i].c, &a[i].d);
		}
		std::sort(a + 1, a + m + 1);
		ans = powx(v, 2 * (a[1].c - 1)) * powx(v, 2 * (n - a[m].c)) % mod;
		for(int i = 1; i < m; i++) {
			if(a[i].c == a[i + 1].c) {
				if(a[i].d != a[i + 1].d) {
					ans = 0; break;
				}
				continue;
			}
			ll t = powx(v, a[i + 1].c - a[i].c - 1) * (v - 1) % mod;
			ans = ans * (powx(v, 2 * (a[i + 1].c - a[i].c)) - t + mod) % mod;
		}
		printf("%lld\n", ans);
	}
	return 0;
}

