#include <bits/stdc++.h>
using i64 = __int128;

constexpr i64 P = 1e9 + 7;

i64 ksm(i64 base, i64 b)
{
    i64 res = 1;

    while (b)
    {
        if (b & 1)
            res = res * (base % P) % P;
        base = ((base % P) * (base % P) % P) % P;
        b >>= 1;
    }

    return res;
}

template <typename T>
void read(T &x)
{
    x = 0;
    T f = 1;
    char ch = getchar();
    while (!isdigit(ch))
    {
        if (ch == '-')
            f = -1;
        ch = getchar();
    }
    while (isdigit(ch))
    {
        x = (x << 3) + (x << 1) + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
}

template <typename T, typename... Tp>
void read(T &x, Tp &...rest)
{
    read(x);
    read(rest...);
}

template <typename T>
void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }

    if (x >= 10)
        write(x / 10);
    putchar(x % 10 + '0');
}

void solve()
{
    i64 n, m;
    i64 v;
    read(n, m, v);
    i64 res = 1;

    std::unordered_map<int, int> vis;
    std::vector<int> restr{};
    for (int i = 1; i <= m; ++i)
    {
        int c, d;
        std::cin >> c >> d;
        if (vis.find(c) != vis.end())
        {
            if (vis[c] != d)
            {
                puts("0");
                return;
            }
            vis[c] = d;
        }
        else
        {
            vis[c] = d;
            restr.emplace_back(c);
        }
    }

    if (restr.size() == 1)
    {
        res = res * (ksm(v, 2LL * (n - 1LL)) % P) % P;
        write(res);
        puts("");
        return;
    }

    std::sort(restr.begin(), restr.end());
    std::vector<std::pair<int, int>> segs;
    for (int i = 0; i < (int)restr.size() - 1; ++i)
        segs.emplace_back(restr[i], restr[i + 1]);

    res = res * (ksm(v, 2 * (segs[0].first - 1)) % P) % P;
    res = res * (ksm(v, 2 * (n - segs.back().second)) % P) % P;
    for (auto s : segs)
    {
        auto l = s.first, r = s.second;
        if (r == l + 1)
            res = res * (v * (v - 1) + 1) % P;
            // assert(res > 0);
        else
            res = res * (ksm(v, r - l - 1) % P) * ((ksm(v, r - l + 1) - 1) % P) % P;
            // assert(res > 0);
    }

    write(res % P);
    puts("");
    return;
}

int main()
{
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    int t;
    std::cin >> t;
    while (t--)
        solve();

    return 0;
}