#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define IOS ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
using namespace std;
const int N = 1e5 + 10, mod = 1e9 + 7;
int T, n, m, v, c[N], d[N];
bool vis[N];
vector<pii> vec;
int qpow(int x, int y) {
    if (y < 0)
        return 1;
    int ret = 1;
    for (; y; y >>= 1, x = x * x % mod)
        if (y & 1)
            ret = ret * x % mod;
    return ret;
}
void solve() {
    cin >> n >> m >> v;
    if (!m) {
        cout << qpow(v, 2 * n) << "\n";
        return;
    }
    vec.clear();
    memset(c, 0, sizeof c);
    memset(d, 0, sizeof d);
    for (int i = 1; i <= m; i++) {
        cin >> c[i] >> d[i];
        vec.push_back({c[i], d[i]});
    }
    sort(vec.begin(), vec.end());
    for (int i = 1; i < vec.size(); i++)
        if (vec[i].first == vec[i - 1].first and vec[i].second != vec[i - 1].second) {
            cout << "0\n";
            return;
        }
    sort(c + 1, c + m + 1);
    int tmp = unique(c + 1, c + m + 1) - c - 1;
    int ans = 1;
    for (int i = 2; i <= tmp; i++) {
        int len = c[i] - c[i - 1];
        ans = ans * ((qpow(v, 2 * len) - qpow(v, len) + qpow(v, len - 1) + mod) % mod) % mod;
    }
    ans = ans * qpow(v, (c[1] - 1) * 2) % mod * qpow(v, (n - c[tmp]) * 2) % mod;
    cout << ans << "\n";
}
signed main() {
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    IOS;
    cin >> T;
    while (T--)
        solve();
    return 0;
}
/*
1
7 1 2
4 1
*/