#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int T, n, m, v, x, y, a[1000005], ans, dp[1000005][2], p[1000005], q[1000005];
int qpow(int x, int y)
{
	int res = 1;
	while(y)
	{
		if(y & 1) res = res * x % mod;
		x = x * x % mod;
		y >>= 1;
	}
	return res;
}
int dfs(int x, int now)
{
	if(now && a[x] && a[x] != now) return 0;
	if(x == n) return 1;
	if(!now && !a[x]) return 4 * dfs(x + 1, 0);
	else return dfs(x + 1, 1) + dfs(x + 1, 2) + 2 * dfs(x + 1, 0);
}
signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%lld", &T);
	while(T--)
	{
		memset(a, 0, sizeof(a));
		scanf("%lld%lld%lld", &n, &m, &v);
		bool flag = 0, pd = 0;
		ans = 1;
		for(int i = 1; i <= m; i++)
		{
			scanf("%lld%lld", &x, &y);
			p[i] = x, q[i] = y;
			if(m == 1) continue;
			if(x != i) pd = 1;
			if(n > 1000000) continue;
			if(!a[x]) a[x] = y;
			else if(a[x] != y)
			{
				flag = 1;
				break;
			}
		}
		if(m == 1)
		{
			int res = v * v % mod;
			printf("%lld\n", qpow(res, n - 1));
			continue;
		}
		if(flag) 
		{
			printf("0\n");
			continue;
		}
		if(v <= 2)
		{
			printf("%lld\n", dfs(1, 0));
			continue;
		}
		if(!pd)
		{
			int res = 1;
			for(int i = 1; i < n; i++)
			{
				int x = ((v - 1) * v % mod + 1ll) % mod;
				res = res * x % mod;
			}
			printf("%lld\n", res);
			continue;
		}
		sort(p + 1, p + m + 1);
		ans = qpow(v, p[1] + p[1] - 2ll);
		int lst = p[1];
		for(int i = 2; i <= m; i++)
		{
			if(p[i] == lst + 1 || p[i] == lst) 
			{
				lst = p[i];
				continue;
			}
			int k = p[i] - lst - 1ll;
			ans = ans * (qpow(v, 2ll * k + 2ll) - qpow(v, k) * (v - 1) % mod + mod) % mod; 
			lst = p[i];
		}
		ans = ans * qpow(v, 2ll * (n - p[m])) % mod;
		printf("%lld\n", ans);
	}
	return 0;
}

