#include <bits/stdc++.h>
#define al_inline() inline

namespace wor {
        std::ifstream fin("assign.in");
        std::ofstream fout("assign.out");
}

constexpr uint64_t mod = 1000000007;

std::unordered_map<size_t, uint32_t> nums;

size_t n, m;
uint64_t v;

al_inline() uint64_t power(const uint64_t &x, size_t y) {
        uint64_t r = 1, t = x;
        while (y) {
                if (y & 1) r = (r * t) % mod;
                t = (t * t) % mod;
                y >>= 1;
        }
        return r;
}

size_t back;

al_inline() uint64_t solve() {
        uint64_t la, ls;
        la = ls = power(v * v % mod, n - back);
        uint64_t ta, ts;
        for (size_t i = back - 1; i > 0; --i) {
                ta = la;
                ts = ls;
                if (nums.count(i)) {
                        la = ls = (ta * v % mod * (v - 1) % mod + ts * (nums.count(i + 1) ? 1 : v) % mod) % mod;
                } else {
                        la = ta * v % mod * v % mod;
                        ls = ts * v % mod + ta * v % mod * (v - 1) % mod;
                }
        }
        return la;
}

signed main() {
        std::ios_base::sync_with_stdio(false);
        std::cin.tie(0);
        std::cout.tie(0);
        size_t T;
        wor::fin >> T;
        size_t x;
        uint32_t y;
        bool failed;
        while (T--) {
                wor::fin >> n >> m >> v;
                failed = false;
                back = 0;
                for (size_t i = 0; i < m; ++i) {
                        wor::fin >> x >> y;
                        if (nums.count(x) && nums[x] != y) failed = true;
                        nums[x] = y;
                        back = std::max(back, x);
                }
                m = nums.size();
                if (failed) { wor::fout << "0\n"; }
                else { wor::fout << solve() << '\n'; }
                wor::fout.flush();
                nums.clear();
        }
        return 0;
}


