#include <bits/stdc++.h>
#define fst first
#define scd second
#define pb emplace_back
#define mkp make_pair
#define uint unsigned
#define mems(a, x) memset((a), (x), sizeof(a))

using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef unsigned long long ull;
typedef pair<ll, ll> pii;

inline int read() {
	int x = 0;
	char c = getchar();
	bool f = 0;
	while (c < '0' || c > '9') {
		f |= (c == '-');
		c = getchar();
	}
	while ('0' <= c && c <= '9') {
		x = x * 10 + c - '0';
		c = getchar();
	}
	return f ? -x : x;
}

const int maxn = 100100;
const ll mod = 1000000007;

inline ll qpow(ll b, ll p) {
	ll res = 1;
	while (p) {
		if (p & 1) {
			res = res * b % mod;
		}
		b = b * b % mod;
		p >>= 1;
	}
	return res;
}

ll n, m, K;
pii a[maxn];

void solve() {
	n = read();
	m = read();
	K = read();
	for (int i = 1; i <= m; ++i) {
		a[i].fst = read();
		a[i].scd = read();
	}
	sort(a + 1, a + m + 1);
	for (int i = 1; i < m; ++i) {
		if (a[i].fst == a[i + 1].fst && a[i].scd != a[i + 1].scd) {
			puts("0");
			return;
		}
	}
	m = unique(a + 1, a + m + 1) - a - 1;
	ll ans = qpow(K * K % mod, a[1].fst - 1 + n - a[m].fst);
	for (int i = 1; i < m; ++i) {
		ll res = qpow(K * K % mod, a[i + 1].fst - a[i].fst);
		res = (res - qpow(K, a[i + 1].fst - a[i].fst - 1) * (K - 1) % mod + mod) % mod;
		ans = ans * res % mod;
	}
	printf("%lld\n", ans);
}

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T = read();
	while (T--) {
		solve();
	}
	return 0;
}