#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <map>
#include <set>
#include <unordered_map>
#define rainn 0x3f3f3f3f

using namespace std;

struct Node
{
    int cnt[2];
    int to;
};

const long long mod = 1e9 + 7;
int T;
long long n, m, v;
map<int, int> mp;

void write(__int128 x)
{
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}

long long qpow(long long x, long long y)
{
    long long res = 1;
    while (y)
    {
        if (y & 1)
            res = res * x % mod;
        y >>= 1;
        x = x * x % mod;
    }
    return res;
}

int main()
{
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> T;
    while (T--)
    {
        int res = 0;
        mp.clear();
        cin >> n >> m >> v;
        bool flag = true;
        for (int i = 1; i <= m; i++)
        {
            int x, y;
            cin >> x >> y;
            if (mp.count(x) and mp[x] != y)
                flag = false;
            mp[x] = y;
        }
        if (!flag)
        {
            cout << 0 << "\n";
            continue;
        }
        int cnt = 0, lst = -1;
        for (auto i : mp)
        {
            if (i.first == lst + 1)
                cnt++;
            lst = i.first;
        }
        n--;
        cout << qpow(v * v % mod, n - cnt) * qpow((v * v - v + 1) % mod, cnt) % mod << "\n";
    }

    return 0;
}
/*
10 11 2
2 2
3 2
4 2
7 2
10 2

_ 2 2 2 _ _ 2 _ _ 2

5
4 3 2
2 2
3 2
4 2
59
5 3 2
2 2
3 2
4 2
236
3 3 2
2 2
3 2
2 2
15
2 1 2
2 2
4

*/