#include typedef long long ll; using std::min; using std::max; #define rd() read() #define lowbit(x) (-x & x) #define E(i, l, r) for (int i = l; i <= r; ++ i) #define FILE(filename) { \ freopen(#filename ".in", "r", stdin); \ freopen(#filename ".out", "w", stdout); \ } template inline T read() { T x = 0; bool f = 0; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') f = 1; c = getchar();} while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return f ? -x : x; } template void write(T x) { if (x < 0) putchar('-'), x = -x; if (x / 10) write(x / 10); putchar((x % 10) ^ 48); } namespace pigeon { const int N = 1e5 + 5; const int mod = 1e9 + 7; int n, m, v; std::pair a[N]; int qpow(int x, int y = mod - 2) { int res = 1; while (y) { if (y & 1) res = 1ll * res * x % mod; x = 1ll * x * x % mod; y >>= 1; } return res; } void Main() { n = rd(); m = rd(); v = rd(); E(i, 1, m) { int x, y; x = rd(); y = rd(); a[i] = std::make_pair(x, y); } int ans = 1, sum = n - 1; std::sort(a + 1, a + m + 1); E(i, 1, m - 1) if (a[i].first == a[i + 1].first && a[i].second != a[i + 1].second) { write(0); puts(""); return; } E(i, 1, m - 1) { int len = a[i + 1].first - a[i].first; sum -= len; if (len == 0) continue; int tmp = 1ll * (v - 1) * qpow(v, len - 1) % mod; ans = 1ll * ans * (qpow(1ll * v * v % mod, len) - tmp + mod) % mod; } write(1ll * ans * qpow(1ll * v * v % mod, sum) % mod); puts(""); } } int main() { FILE(assign); int T = rd(); while (T --) pigeon::Main(); return 0; }