#include <bits/stdc++.h>
using namespace std;
int read() {
    char ch = getchar();
    int x = 0;
    while (ch < '0' || ch > '9') ch = getchar();
    while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
    return x;
}

namespace Solve {
    const int M = 1e5 + 10, mod = 1e9 + 7;
    int qpow(int b, int p) {
        int ret = 1;
        for (; p; p >>= 1) {
            if (p & 1) ret = 1ll * ret * b % mod;
            b = 1ll * b * b % mod;
        }
        return ret;
    }

    int n, m, v;
    struct Node {
        int c, d;
    } x[M];

    void mian() {
        n = read(), m = read(), v = read();
        for (int i = 1; i <= m; i++) x[i].c = read(), x[i].d = read();
        if (v == 1) return printf("1\n"), void();

        sort(x + 1, x + 1 + m, [](Node x, Node y) { return x.c < y.c; });
        for (int i = 1; i < m; i++)
            if (x[i].c == x[i + 1].c && x[i].d != x[i + 1].d) {
                printf("0\n");
                return ;
            }

        int ans = 1;
        for (int i = 1; i < m; i++) {
            if (x[i + 1].c == x[i].c) continue;
            if (x[i + 1].c == x[i].c + 1) {
                int s = (1ll * (v - 1) * v + 1) % mod;
                ans = 1ll * s * ans % mod;
            } else {
                int A = x[i].c, B = x[i + 1].c;
                int t = qpow(v, 2 * (B - A)) - 1ll * qpow(v, B - A - 1) * (v - 1) % mod;
                t = (t + mod) % mod;
                ans = 1ll * t * ans % mod;
            }
        }
        ans = 1ll * ans * qpow(v, 2 * (x[1].c - 1)) % mod;
        ans = 1ll * ans * qpow(v, 2 * (n - x[m].c)) % mod;
        printf("%d\n", ans);
    }
}

int main() {
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    int T = read();
    while (T--) Solve::mian();
    return 0;
}