#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define mkp make_pair
#define pii pair<int,int>
#define debug(x) cout << #x << " = " << x << "\n";

bool MemoryST;

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int id[N];
int n, m, v, c[N], d[N], f[N]; 

int ksm (int x, int y) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % mod;
		x = 1ll * x * x % mod; y >>= 1;
	}
	return res;
}

void Mainsolve() {
	cin >> n >> m >> v;
	for (int i = 1; i <= m; ++i)
		cin >> c[i] >> d[i], id[i] = i;
	sort (id + 1, id + m + 1, [&](int x, int y) {
		return c[x] < c[y];
	}); 
	for (int i = 1; i < m; ++i) {
		int x = id[i], y = id[i + 1];
		if (c[x] == c[y] && d[x] != d[y]) {
			cout << 0 << "\n"; return ;
		}
	}
	sort (c + 1, c + m + 1);
	m = unique(c + 1, c + m + 1) - c - 1;
	int ans = 1;
	ans = 1ll * ans * ksm(v, 2 * (c[1] - 1)) % mod;
	ans = 1ll * ans * ksm(v, 2 * (n - c[m])) % mod;
	for (int i = 1; i < m; ++i) {
		int d = c[i + 1] - c[i];
		int s = (ksm(v, 2 * d) + mod - 1ll * (v - 1) * ksm(v, d - 1) % mod) % mod;
		ans = 1ll * ans * s % mod;
	}
	cout << ans << "\n";
}

bool MemoryED;

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	cerr << fixed << setprecision(6) << (&MemoryST - &MemoryED) / 1024.0 / 1024.0 << "MB\n";

	int T; cin >> T;
	while (T--) Mainsolve();

	cerr << 1e3 * clock() / CLOCKS_PER_SEC << "ms\n";
}
