#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second

using namespace std;
using ll = long long;
using pii = pair<int, int>;

void SetIO(string s = "")
{
    if (s != "")
    {
        freopen((s + ".in").c_str(), "r", stdin);
        freopen((s + ".out").c_str(), "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);
}

const ll MOD = 1e9 + 7;
ll add(ll x, ll y){return (x += y) >= MOD && (x -= MOD), x;}
void Add(ll& x, ll y){x = add(x, y);}
ll sub(ll x, ll y){return (x -= y) < 0 && (x += MOD), x;}
void Sub(ll& x, ll y){x = sub(x, y);}
ll Pow(ll x, ll y)
{
    ll res = 1;
    for (; y; x = x * x % MOD, y >>= 1)if (y & 1)
        res = res * x % MOD;
    return res;
}

const int N = 1e5 + 5;

struct Node{
    int pos, val;
    bool operator < (const Node& rhs) const{return pos < rhs.pos;}
}a[N];

int T, n, m;
ll v, ans, all;

signed main()
{
    SetIO("assign");
    cin >> T;
    while(T--)
    {
        cin >> n >> m >> v;
        ll invv = Pow(v, MOD - 2);
        ans = all = 0;
        for (int i = 1, x, y; i <= m; i++)
            cin >> a[i].pos >> a[i].val;
        sort(a + 1, a + m + 1);
        bool bj = false;
        for (int i = 1; i < m; i++)if (a[i].pos == a[i + 1].pos && a[i].val != a[i + 1].val)
            bj = true;
        if (bj)
        {
            cout << "0\n";
            continue;
        }
        for (int i = 1; i < m; i++)
        {
            if (a[i].pos == a[i + 1].pos)
                continue;
            ll pre = sub(Pow(v, 2 * (a[i].pos - 1)), ans * Pow(invv, 2 * (n - a[i].pos)) % MOD);
            Add(ans, pre * Pow(v, a[i + 1].pos - a[i].pos - 1) % MOD * (v - 1) % MOD * Pow(v, 2 * (n - a[i + 1].pos)) % MOD);
        }
        cout << sub(Pow(v, 2 * (n - 1)), ans) << '\n';
    }
}