#include <iostream>
#include <fstream>
#include <cstdint>
#include <cstddef>
#include <string>
#include <vector>
#include <algorithm>
#include <cassert>

#define FILE
#define FILE_NAME "assign"

#define type using
#define fn auto
#define letmut auto
#define let auto const
#define String std::string
#define Vec std::vector

#ifdef FILE
std::ifstream in(FILE_NAME ".in");
std::ofstream out(FILE_NAME ".out");
#else
#define in std::cin
#define out std::cout
#endif // FILE

type i32 = std::int32_t;
type i64 = std::int64_t;
type u64 = std::uint64_t;
type usize = std::size_t;

const usize MAX_N = 1e5 + 10;
const i64 MOD = 1e9 + 7;

u64 t;
i64 n, m, v;

i64 c[MAX_N];
i64 d[MAX_N];

i64 qpow(i64 a, i64 b) {
	if (b == 0) {
		return 1;
	} else if (b % 2 == 0) {
		return qpow(a * a % MOD, b / 2);
	} else {
		return qpow(a, b - 1) * a % MOD;
	}
}

void solve1() {
	for (usize i = 1; i <= m; ++i) {
		in >> c[i] >> d[i];
	}
//	out << "HI";
	out << qpow((v * v % MOD - (v - 1) + MOD) % MOD, n - 1) << "\n";
}
struct Node {
	i64 c{};
	i64 d{};
	
	Node() {}
	Node(i64 c, i64 d): c(c), d(d) {}
	
	bool operator ==(const Node& rgt) {
		return c == rgt.c && d == rgt.d;
	}
}; 
Node cont[MAX_N];
void solve2() {
	i64 cc, dd;
	i64 ans = 1;
	
	for (usize i = 1; i <= m; ++i) {
		in >> cc >> dd;
		cont[i] = (Node){cc, dd};
	}
	std::sort(cont + 1, cont + 1 + m, [](const Node& lft, const Node& rgt) -> bool {
		if (lft.c == rgt.c) {
			return lft.d < rgt.d;
		}
		return lft.c < rgt.c;
	});
	for (usize i = 1; i < m; ++i) {
		if (cont[i].c == cont[i + 1].c && cont[i].d != cont[i + 1].d) {
			out << "0\n";
			return;
		}
	}
	let len = std::unique(cont + 1, cont + m + 1) - cont - 1;
	for (usize i = 1; i < len; ++i) {
		ans *= (qpow(v * v % MOD, cont[i + 1].c - cont[i].c) - (v - 1) * qpow(v, cont[i + 1].c - cont[i].c - 1) % MOD + MOD) % MOD;
		ans %= MOD;
	}
	if (cont[1].c != 1) {
//		out << "TT ";
		ans *= (qpow(v * v % MOD, cont[1].c - 1)) % MOD;
		ans %= MOD;
	}
	if (cont[len].c != n) {
		
		ans *= qpow(v * v % MOD, n - cont[len].c);
		ans %= MOD; 
	}
	
	out << ans << "\n";
}

fn main() -> i32
{
//	int a[13] = {0, 1, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6};
//	int b = std::unique(a + 1, a + 13) - a;
//	for (let v : a) {
//		std::cerr << v << " ";
//	}
//	std::cerr << "\n" << b << "\n";
	in >> t;
	while(t--) {
		in >> n >> m >> v;
//		if (n == m) {
//			solve1();
//		} else {
			solve2();
//		}
	}
    return 0;
}
