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

using ll = long long;

constexpr int N = 1e5 + 5;
constexpr ll MOD = 1e9 + 7;

int t, n, m, v;
map<int, int> mp;

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

int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0), cin.tie(0);
	cin >> t;
	while (t--)
	{
		mp.clear();
		bool tag = 0;
		cin >> n >> m >> v;
		int fir = (int)1e9;
		for (int i = 1; i <= m; i++)
		{
			int x, y;
			cin >> x >> y;
			if (mp.count(x) && mp[x] != y) tag = 1;
			mp[x] = y;
			fir = min(fir, x);
		}
		if (tag)
		{
			cout << "0\n";
			continue;
		}
		ll ans = qpow(v, 2 * (fir - 1));
		for (int i = fir; i <= n; i++)
		{
			auto it = mp.find(i);
			int nxt = (it == prev(mp.end()) ? -1 : next(it)->first);
			if (nxt == -1)
			{
				if (i == n) break;
				ans = ans * qpow(v, 2 * (n - i)) % MOD;
				break;
			}
			else
			{
				int x = i;
				ll tot = qpow(v, 2 * (nxt - x));
				tot = tot - qpow(v, nxt - x - 1) * (v - 1) % MOD;
				tot = (tot + MOD) % MOD;
				ans = ans * tot % MOD;
			}
			i = nxt - 1;
		}
		cout << ans << "\n";
	}
	return 0;
}
