// Lucky_Luo NOIP2024 rp+=inf;
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
using namespace std;
typedef long long ll;
typedef double db;
const int N = 1e5 + 7, mo = 1e9 + 7;
int i, j, k, l, r, m, n, idx, cnt;
int read()
{
	int f = 1, x = 0; char ch = getchar();
	while (!isdigit(ch)) 
	{
		if (ch == '-') f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) x = 10 * x + ch - '0', ch = getchar();
	return f * x;
}
int qp(int a, int b=mo-2)
{
	int res = 1;
	for (; b; b>>=1,a=1ll*a*a%mo) if (b&1) res = 1ll * res * a % mo;
	return res;
}
struct node 
{
	int p, c;
	#define p(x) d[x].p
	#define c(x) d[x].c
	bool operator < (const node&x) const {return p == x.p ? c < x.c : p < x.p;}
} d[N];
void solve()
{
	n = read(), m = read(), k = read();
	for (i=1; i<=m; i++) p(i) = read(), c(i) = read();
	sort(d+1, d+m+1);
	for (i=1; i<m; i++) if (p(i) == p(i+1) && c(i) != c(i+1))
	{
		puts("0");
		return;
	}
	int ans = 1ll * qp(k, 2*(p(1)-1)) * qp(k, 2*(n-p(m))) % mo;
	for (i=1; i<m; i++) if (p(i) != p(i+1)) ans = 1ll * (qp(k, 2*(p(i+1)-p(i))) + mo - 1ll * qp(k, p(i+1)-p(i)-1) * (k-1) % mo) % mo * ans % mo;
	printf("%d\n", ans);
}
signed main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	int T = read();
	while (T--) solve(); 
	return 0;
}

