#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;

ll ans, tot;
ll n, m, v;
map<ll, ll> a;
ll b[N];

ll ksm(ll a, ll b)
{
    a %= MOD;
    ll res = 1;
    while (b)
    {
        if (b & 1)
        {
            res = res * a % MOD;
        }
        a = a * a % MOD;
        b >>= 1;
    }
    return res;
}

void work()
{
    memset(b, 0, sizeof(ll) * N);
    a.clear();

    cin >> n >> m >> v;
    bool flag = 0;
    for (int i = 1, c, d; i <= m; i++)
    {
        cin >> c >> d;
        if (a.count(c) > 0)
        {
            if (a[c] == d)
            {
                i--, m--;
                continue;
            }
            flag = 1;
        }
        a[c] = d;
        b[i] = c;
    }
    if (flag)
    {
        cout << "0\n";
        return;
    }
    // if (m == 1)
    // {
    //     ans = ksm(v * v, n - 1);
    //     cout << ans << '\n';
    //     return;
    // }
    ans = 1;
    sort(b + 1, b + 1 + m);
    for (int i = m; i > 1; i--)
    {
        ans = ans * ((ksm(v * v, b[i] - b[i - 1]) - (v - 1) * ksm(v, b[i] - b[i - 1] - 1) % MOD) % MOD + MOD) % MOD;
    }
    ans = ans * ksm(v * v, (n - b[m])) % MOD;
    ans = ans * ksm(v * v, b[1] - 1) % MOD;
    cout << ans << '\n';
    return;
}

int main()
{
    cin.tie(0)->sync_with_stdio(0);
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);

    int T;
    cin >> T;
    while (T--)
    {
        work();
    }
    return 0;
}