#include <cstdio>
#include <algorithm>
#define maxm 100000
#define int long long
const int mod = 1e9 + 7;

inline int mpow(const int &n, const int &p) {
	int ans = 1;
	for (int np = n, lp = p; lp; lp >>= 1, (np *= np) %= mod)
		if (lp & 1) (ans *= np) %= mod;
	return ans;
}
struct pt {
	int wh, a;
} ch[maxm + 10];
inline int cmp(const pt &a, const pt &b) {
	return a.wh < b.wh;
}
int T, n, m, v, vv, len, ans, mnow;

signed main(void) {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);
	scanf("%lld", &T);
	while (T--) {
		scanf("%lld %lld %lld", &n, &m, &v);
		for (int i = 1; i <= m; i++)
			scanf("%lld %lld", &ch[i].wh, &ch[i].a);
		std :: sort(ch + 1, ch + m + 1, cmp);
		for (int i = 1; i < m; i++)
			if (ch[i].wh == ch[i + 1].wh && ch[i].a != ch[i + 1].a) {
				puts("0"); goto end;
			}
		len = 1; vv = v * v % mod;
		for (int i = 2; i <= m; i++)
			if (ch[i].wh != ch[i - 1].wh) ch[++len] = ch[i];
//		for (int i = 1; i <= len; i++)
//			printf("%lld %lld %lld\n", i, ch[i].wh, ch[i].a);
		ans = mpow(vv, n - 1), mnow = mpow(vv, ch[1].wh - 1); 
		for (int i = 2; i <= len; i++) {
			//printf("i = %lld, %lld %lld\n", i, now, ans);
			int ad = mpow(v, ch[i].wh - ch[i - 1].wh - 1) * (v - 1) % mod;
			ans = ((ans - mnow * ad % mod * mpow(vv, n - ch[i].wh) % mod) % mod + mod) % mod;
			mnow = mnow * (mpow(vv, ch[i].wh - ch[i - 1].wh) - ad) % mod;
		}
		printf("%lld\n", ans);
		end:;
	}
	
	return 0;
}
