#include <algorithm>
#include <array>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <sys/types.h>
#include <tuple>
#include <vector>

#define let const auto
#define mut auto

template<typename T>
using vec = std::vector<T>;
using ull = unsigned long long;
using ll = long long;

using std::tuple, std::array;

template<typename T>
T read() {
	T s = 0, w = 1, c = getchar();
	while (!isdigit(c)) {
		if (c == '-') {
			w = -1;
		}
		c = getchar();
	}
	while (isdigit(c)) {
		s = s * 10 + c - '0';
		c = getchar();
	}
	return s * w;
}

using Limit = tuple<uint, uint>;

class Config {
public:
	uint n, m, v;
	vec<Limit> limits;
};

uint solve(const Config &);

int main() {
	freopen("assign.in", "r", stdin);
	freopen("assign.out", "w", stdout);

	mut T = read<uint>();
	while (T--) {
		let n = read<uint>(), m = read<uint>(), v = read<uint>();
		mut config = Config{n, m, v, vec<Limit>(m)};
		for (mut &[c, d] : config.limits) {
			c = read<uint>(), d = read<uint>();
		}
		let ans = solve(config);
		printf("%u\n", ans);
	}

	fclose(stdin);
	fclose(stdout);
	return 0;
}

constexpr uint MOD = 1'000'000'007;

uint add(const uint &a, const uint &b) {
	return a + b >= MOD ? a + b - MOD : a + b;
}

uint mul(const uint &a, const uint &b) {
	return static_cast<uint>(static_cast<ull>(a) * b % MOD);
}

array<array<uint, 2>, 2> mul(const array<array<uint, 2>, 2> &a, const array<array<uint, 2>, 2> &b) {
	array<array<uint, 2>, 2> res{array<uint, 2>{0, 0}, {0, 0}};
	res[0][0] = add(mul(a[0][0], b[0][0]), mul(a[1][0], b[0][1]));
	res[0][1] = add(mul(a[0][1], b[0][0]), mul(a[1][1], b[0][1]));
	res[1][0] = add(mul(a[0][0], b[1][0]), mul(a[1][0], b[1][1]));
	res[1][1] = add(mul(a[0][1], b[1][0]), mul(a[1][1], b[1][1]));
	return res;
}

vec<array<array<uint, 2>, 2>> pre;

void init(const uint v) {
	array<array<uint, 2>, 2> base{array<uint, 2>{mul(v, v), mul(v, (v - 1))}, {0, v}};
	vec<array<array<uint, 2>, 2>> p(31);
	p[0] = base;
	for (uint i = 1; i < 31; i++) {
		p[i] = mul(p[i - 1], p[i - 1]);
	}
	pre = p;
}

void turn(uint &a, uint &b, uint k) {
	uint pos = 0;
	while (k) {
		if (k & 1) {
			let ta = add(mul(a, pre[pos][0][0]), mul(b, pre[pos][0][1])), tb = add(mul(a, pre[pos][1][0]), mul(b, pre[pos][1][1]));
			a = ta, b = tb;
		}
		pos++;
		k >>= 1;
	}
}

uint solve(const Config &config) {
	if (config.n > 5'000'000) {
		init(config.v);

		mut limits = config.limits;
		std::sort(limits.begin(), limits.end(), [](const Limit &a, const Limit &b){
			return std::get<0>(a) < std::get<0>(b);
		});

		mut lst = -1u, lst_val = -1u;
		for (mut &[c, d] : limits) {
			if (lst == c && lst_val != d) {
				return 0;
			}
			lst = c;
			lst_val = d;
		}

		bool flag = false;
		mut a = 0u, b = 0u;
		if (std::get<0>(limits[0]) == 1) {
			a = config.v - 1;
			b = 1;
			flag = true;
		} else {
			a = config.v;
			b = 0;
		}

		mut now = 0u;

		for (mut &[c, d] : limits) {
			if (flag) {
				flag = false;
				continue;
			}
			if (c == config.n) {
				break;
			}
			c -= 1;
			if (now + 1 < c) {
				turn(a, b, (c - 1) - now);
			}
			let ta = mul(add(mul(a, config.v), b), config.v - 1), tb = add(mul(a, config.v), b);
			a = ta, b = tb;
			now = c;
		}

		if (now + 2 < config.n) {
			turn(a, b, (config.n - 2) - now);
		}
		if (std::get<0>(limits.back()) == config.n) {
			return add(b, mul(a, config.v));
		} else {
			return add(mul(b, config.v), mul(a, config.v));
		}	
	}

	vec<array<uint, 2>> f(config.n);
	vec<uint> a(config.n, -1);
	for (mut &[c, d] : config.limits) {
		if (a[c - 1] != -1 && a[c - 1] != d) {
			return 0;
		}
		a[c - 1] = d;
	}
	if (a[0] != -1) {
		f[0] = {config.v - 1, 1};
	} else {
		f[0] = {config.v, 0};
	}
	for (mut i = 1; i < config.n - 1; i++) {
		if (a[i] != -1) {
			f[i][0] = mul(add(mul(f[i - 1][0], config.v), f[i - 1][1]), config.v - 1);
			f[i][1] = add(mul(f[i - 1][0], config.v), f[i - 1][1]);
		}else {
			f[i][0] = mul(add(mul(f[i - 1][0], config.v), mul(f[i - 1][1], config.v - 1)), config.v);
			f[i][1] = mul(f[i - 1][1], config.v);
		}
	}
	mut res = 0u;
	if (a.back() != -1) {
		res = add(f[config.n - 2][1], mul(f[config.n - 2][0], config.v));
	} else {
		res = add(mul(f[config.n - 2][1], config.v), mul(f[config.n - 2][0], config.v));
	}

	return res;
}