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

const int N = 100010;
const int mod = 1e9 + 7;

struct node
{
	int c, x;
	
	bool operator < (const node &u) const
	{
		return c < u.c;
	}
} a[N];

int t, n, m, v, cnt;
int c[N];

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

signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d%d%d", &n, &m, &v);
		for (int i = 1; i <= m; i++) scanf("%d%d", &a[i].c, &a[i].x);
		sort(a + 1, a + m + 1);
		bool flag = 0;
		cnt = 1;
		c[1] = a[1].c;
		for (int i = 2; i <= m; i++)
			if (a[i].c == a[i - 1].c && a[i].x != a[i - 1].x)
			{
				flag = 1;
				break;
			}
			else if (a[i].c != a[i - 1].c) c[++cnt] = a[i].c;
		if (flag)
		{
			printf("0\n");
			continue;
		}
		ll ans = 1;
		for (int i = 2; i <= cnt; i++)
			ans = ans * ((qpow(v, 2 * (c[i] - c[i - 1])) - qpow(v, c[i] - c[i - 1] - 1) * (v - 1) % mod + mod) % mod) % mod;
		ans = ans * qpow(v, 2 * (n - c[cnt] + c[1] - 1)) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}
