#include <bits/stdc++.h>
#define int __int128
using namespace std;
inline int read()
{
    int ans = 0;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        c = getchar();
    }
    while (c >= '0' && c <= '9')
    {
        ans = ans * 10 + c - '0';
        c = getchar();
    }
    return ans;
}
inline void write(int x)
{
    if (x == 0)
        return;
    write(x / 10);
    putchar('0' + (x % 10));
}
const int MOD = 1000000007;
map<int, int> l;
//
inline int mypow(int x, int t)
{
    int ans = 1, p = 0;
    while (t >= (1 << p))
    {
        if (t & (1 << p++))
            ans *= x;
        x *= x;
        x %= MOD;
        ans %= MOD;
    }
    return ans;
}
int solve()
{
    int n = read(), m = read(), v = read(), ans = 1;
    l.clear();
    for (int i = 0, x, y; i < m; i++)
    {
        x = read(), y = read();
        if (l[x] == 0)
        {
            l[x] = y;
        }
        else if (l[x] != y)
            return 0;
    }
    int last = 0, t = v * v;
    for (auto i : l)
    {
        int tim = i.first - last;
        if (last == 0)
            ans *= mypow(t, tim - 1);
        else if (tim == 1)
            ans *= t - v + 1;
        else
            ans *= t * (mypow(t, tim-1)-1);
        ans %= MOD;
        last = i.first;
    }
    auto i = l.rbegin();
    ans *= mypow(t, n - i->first);
    return ans % MOD;
}

signed main()
{
    freopen("assign.in", "r", stdin);
    freopen("assign.out","w",stdout);
    int T = read();
    while (T--)
    {
        int ans = solve();
        if (ans == 0)
            putchar('0');
        else
            write(ans);
        putchar('\n');
    }
}
