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

constexpr int MOD = 1000000007;

int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	
	int T;
	cin >> T;
	for (int arona = 1; arona <= T; arona++)
	{
		auto quickPow = [&](int a, int b) -> int
		{
			int p = 1;
			while (b)
			{
				if (b & 1)  p = 1LL * p * a % MOD;
				a = 1LL * a * a % MOD;
				b >>= 1;
			}
			return p;
		};
		struct Arr
		{
			int c, d;
			bool operator < (const Arr &tmp) const
			{
				return c < tmp.c;
			}
		};
		
		int n, m, v, ans = 1;
		bool flag = 1;
		cin >> n >> m >> v;
		vector<Arr> arr(m + 1);
		for (int i = 1; i <= m; i++)  cin >> arr[i].c >> arr[i].d;
		sort(arr.begin() + 1, arr.end());
		for (int i = 1; i < m; i++)
		{
			if (arr[i].c == arr[i + 1].c && arr[i].d != arr[i + 1].d)
			{
				flag = 0;
				break;
			}
		}
		if (!flag)
		{
			cout << "0\n";
			continue;
		}
		ans = 1LL * ans * quickPow(v, 2 * (arr[1].c - 1)) % MOD;
		ans = 1LL * ans * quickPow(v, 2 * (n - arr[m].c)) % MOD;
		for (int i = 1; i < m; i++)
		{
			if (arr[i].c == arr[i + 1].c)  continue;
			int cur = quickPow(v, 2 * (arr[i + 1].c - arr[i].c));
			cur = (1LL * cur - 1LL * quickPow(v, arr[i + 1].c - arr[i].c - 1) * (v - 1) % MOD + MOD) % MOD;
			ans = 1LL * ans * cur % MOD;
		}
		cout << ans << '\n';
	}
	return 0;
}

/*
edit > assign???
*/
