#include <bits/stdc++.h>
typedef long long LL;
typedef std::pair<int, int> pii;
#define fi first
#define se second
#define MP std::make_pair

char buf[1 << 20], *p1, *p2;
#define gc() ((p1 == p2) && ((p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin)), (p1 == p2)) ? 0 : *p1++)
//#define gc() getchar()
int read()
{
	int s = 0, f = 1; char c = gc();
	for (; !isdigit(c); c = gc()) f ^= (c == '-');
	for (; isdigit(c); c = gc()) s = s * 10 + (c ^ 48);
	return f ? s : -s;
}
template<typename T> 
void write(T x, char end = '\n')
{
	if (x < 0) x = -x, putchar('-');
	static int d[100], cur = 0;
	do { d[++cur] = x % 10; } while (x /= 10);
	while (cur) putchar('0' + d[cur--]);
	putchar(end);
}
template<typename T> 
void Fmax(T &x, T y){ if (x < y) x = y; }
template<typename T> 
void Fmin(T &x, T y){ if (y < x) x = y; }
const int MOD = 1e9 + 7;
int fplus(int x, int y){ return x + y >= MOD ? x + y - MOD : x + y; }
int fminus(int x, int y){ return x >= y ? x - y : x + MOD - y; }
void Fplus(int &x, int y){ x = fplus(x, y); }
void Fminus(int &x, int y){ x = fminus(x, y); }
int fpow(int x, int y = MOD - 2)
{
	int res = 1;
	for (; y; y >>= 1, x = (LL)x * x % MOD)
		if (y & 1) res = (LL)res * x % MOD;
	return res;
}
const int MAXN = 100005;
int n, m, V, pos[MAXN], cur;
pii p[MAXN];
int Geo(int x, int K)
{
	if (x == 1) return K;
	return (LL)(fpow(x, K) - 1) * fpow(x - 1) % MOD;
}
void mian()
{
	n = read(), m = read(), V = read();
	for (int i = 1; i <= m; i++)
		p[i].fi = read(), p[i].se = read();
	std::sort(p + 1, p + m + 1);
	int ans = 1;
	cur = 0;
	for (int i = 1, j; i <= m; i = j)
	{
		for (j = i; j <= m && p[j].fi == p[i].fi; j++)
			if (p[j].se != p[i].se) return write(0);
		pos[++cur] = p[i].fi;
	}
	if (pos[1] > 1) ans = (LL)ans * fpow((LL)V * V % MOD, pos[1] - 1) % MOD;
	pos[++cur] = n;
	for (int i = 1; i < cur; i++)
	{
		int len = pos[i + 1] - pos[i];
		if (!len) continue;
		int sum = (LL)(V - 1) * fpow(V, len) % MOD * Geo(V, len) % MOD;
		if (i < cur - 1) Fplus(sum, fpow(V, len - 1));
		else Fplus(sum, fpow(V, len));
		ans = (LL)ans * sum % MOD; 
	}
	write(ans);
}
int main()
{
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	for (int T = read(); T--; ) mian();
	return 0; 
}
