#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second
using namespace std;
const int N = 100010, mod = 1000000007;
int n, m, T, v, t, ans, cnt;
pair <int, int> a[N];
pair <int, int> l[N];
int ksm(int x, int y)
{
	if (y == 0)
		return 1;
	if (y == 1)
		return x;
	if (y == 2)
		return x * x % mod;
	if (y % 2 == 0)
		return ksm(x * x % mod, y / 2) % mod;
	else
		return ksm(x * x % mod, y / 2) * x % mod;
}
signed main()
{
	freopen("assign.in","r",stdin);
	freopen("assign.out","w",stdout);
	cin >> T;
	while(T--)
	{
		scanf("%lld%lld%lld", &n, &m, &v);
		for (int i = 1; i <= m; i++)
			scanf("%lld%lld", &a[i].fi, &a[i].se);
		sort(a + 1, a + 1 + m);
		t = false;
		for (int i = 2; i <= m; i++)
			if (a[i].fi == a[i - 1].fi && a[i].se != a[i - 1].se)
			{
				t = true;
				break;
			}
		if (t)
		{
			printf("0\n");
			continue;
		}
		if (m == 1)
		{
			printf("%lld\n", ksm(v, n) % mod);
			continue;
		}
		else if (m == n)
		{
			ans = 1;
			for (int i = 1; i < m; i++)
				(ans *= (v * (v - 1) + 1) % mod) %= mod;
			printf("%lld\n", ans);
			continue;
		}
		ans = ksm(v, a[1].fi - 1) % mod;
		for (int i = 2; i <= m; i++)
		{
			if (a[i].fi == a[i - 1].fi)
				continue;
			ans *= ksm(v, a[i].fi - a[i - 1].fi + 2) - ksm(v, a[i].fi - a[i - 1].fi + 1) + ksm(v, a[i].fi - a[i - 1].fi) + mod;
			ans %= mod;
		}
		(ans *= ksm(v, n - a[m].fi)) %= mod;
		printf("%lld\n", ans);
	}
	return 0;
}
