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

const long long N = 1e6 + 10, P = 1e9 + 7;
long long T, m, n, v, f[N][2], c[N], d;
map<int, int> flag;

long long qpow(long long x, int k)
{
	long long res = 1;
	while (k)
	{
		if (k & 1) res = res * x % P;
		x = x * x % P;
		k >>= 1;
	}
	return res;
}

int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%lld", &T);
	while (T--)
	{
		for (int i = 1; i <= m; i++) flag[c[i]] = 0;
		for (int i = 1; i <= min(n, N - 1); i++) f[i][0] = f[i][1] = 0;
		scanf("%lld%lld%lld", &n, &m, &v);
		bool fl = 0;
		for (int i = 1; i <= m; i++) {scanf("%lld%lld", &c[i], &d); if (flag[c[i]] && flag[c[i]] ^ d) {
		cout << "0\n"; fl = 1;} flag[c[i]] = d;}
		if (fl) continue;
		
		if (m == 1) {cout << qpow(v * v % P, n - 1) << "\n"; continue;
		}

		if (flag[1]) f[1][1] = 1;
		else f[1][0] = 1;
		for (int i = 1; i < n; i++)
		{
			if (flag[i + 1])
			{
				f[i + 1][1] = (f[i][1] * (((v - 1) * v % P + 1) % P) % P + f[i][0] * v % P * v % P) % P;
			}
			else
			{
				f[i + 1][1] = f[i][1] * v % P;
				f[i + 1][0] = (f[i][0] * v % P * v % P + f[i][1] * (v - 1) % P * v % P) % P;
			}
		}
		cout << (f[n][0] + f[n][1]) % P << "\n";
		
	}
	
	return 0;
}
