#include <bits/stdc++.h>
#define int long long
#define debug(x) cerr << #x << ": " << x << endl
using namespace std;

const int MAXN = 1e6 + 7, mod = 1e9 + 7;
unordered_map<int, int> x;

pair<int, int> inp[MAXN];

int qpow(int u, int v)
{
	int res = 1;
	while (v)
	{
		if (v & 1) res = res * u % mod;
		u = u * u % mod;
		v >>= 1;
	}
	return res;
}

signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
//	ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	int t; cin >> t;
	while (t--)
	{
		x.clear();
		int n, m, v; scanf("%lld%lld%lld", &n, &m, &v); bool zero = false;
		int ans = 1;
		for (int i = 1; i <= m; i++)
		{
			scanf("%lld%lld", &inp[i].first, &inp[i].second);
			int c = inp[i].first, d = inp[i].second;
			if (x[c] != 0 && x[c] != d) {
				zero = true;
				break;
			}
			x[c] = d;
		}
		if (zero) {
			puts("0");
			continue;
		}
		sort(inp + 1, inp + m + 1);
		ans *= qpow(v, 2 * inp[1].first - 2);
		int lst = 1;
		for (int i = 2; i <= m; i++)
		{
			int len = inp[i].first - inp[lst].first;
			if (len == 0) continue;
			int cnt = qpow(v, 2 * len) - qpow(v, len) + qpow(v, len - 1);
			ans = ans * cnt % mod;
			lst = i;
		}
		ans = ans * qpow(v, 2 * (n - inp[m].first)) % mod;
		printf("%lld\n", ans);
	}
}

