#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1e9 + 7;
int T, m, lim;
LL n, a[100005];
map <int, int> mp;
LL ans;
LL Pow(LL a, LL b) {
	LL s = 1;
	while (b) {
		if (b & 1)
			s = s * a % MOD;
		a = a * a % MOD, b = b >> 1;
	}
	return s;
}
int main() {
//	freopen("assign3.in", "r", stdin);
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%d", &T);
	while (T--) {
		scanf("%lld%d%d", &n, &m, &lim);
		mp.clear();
		bool flag = true;
		for (int i = 1, v; i <= m; i++) {
			scanf("%lld%d", &a[i], &v);
			if (!mp.count(a[i]))
				mp[a[i]] = v;
			else if (mp[a[i]] != v)
				flag = false;
		}
		if (!flag) {
			printf("0\n");
			continue;
		}
		sort(a + 1, a + 1 + m);
		ans = Pow(lim, (a[1] - 1) * 2);
		for (int i = 1; i < m; i++)
			if (a[i] != a[i + 1]) {
				LL s = Pow(lim, (a[i + 1] - a[i]) * 2);
				s = (s - Pow(lim, a[i + 1] - a[i] - 1) * (lim - 1) % MOD + MOD) % MOD;
				ans = ans * s % MOD;
			}
		ans = ans * Pow(lim, (n - a[m]) * 2) % MOD;
		printf("%lld\n", ans);
	}
	return 0;
} 
