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

const int mod = 1e9 + 7;
int ttt, n, m, v;

int ksm(int a, int b)
{
	int sum = 1;
	for (; b; b >>= 1)
	{
		if (b & 1)
		{
			sum = sum * a % mod;
		}
		a = a * a % mod;
	}
	return sum;
}

struct node
{
	int c, d;
} t[100005];

bool cmp(node x, node y)
{
	return x.c < y.c;
}

int ji(int x)
{
	return (ksm(v, 2 * x + 2) + ksm(v, x) - ksm(v, x + 1) + mod) % mod;
}

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