// SPDX-License-Identifier: Unlicense
// Author: Bingwu Zhang <xtex@xtexx.eu.org>
#include <bits/stdc++.h>

#define uint unsigned int
#define expT(x) __builtin_expect(x, 1)
#define expF(x) __builtin_expect(x, 0)

namespace xtex
{

    uint rdu()
    {
        uint v = 0;
        char ch = getchar();
        while (!(ch >= '0' && ch <= '9'))
            ch = getchar();
        while (ch >= '0' && ch <= '9')
        {
            v = (v << 3) + (v << 1) + (ch ^ '0');
            ch = getchar();
        }
        return v;
    }

#define MAXN 10000005
// #define MAXN 1000000005
#define MAXM 100005
#define MOD 1000000007

    uint n, m, v;
    static uint vars[MAXN];
    // static std::bitset<MAXN> vars;

    uint sol()
    {
        memset(vars, 0, sizeof(uint) * n);
        // for (uint i = 1; i <= n; ++i)
        //     vars[i] = false;
        // memset(d, 0, sizeof(d));
        n = rdu();
        m = rdu();
        v = rdu();
        if (n >= (MAXN - 5))
        {
            n = 0;
            return 0;
        }
        uint knv = 0;
        for (uint i = 1; i <= m; ++i)
        {
            uint ci = rdu();
            uint di = rdu();
            if (vars[ci] != 0 && vars[ci] != di)
                return 0;
            if (vars[ci] == 0 && ci != 1)
                ++knv;
            vars[ci] = di;
            // vars[ci] = true;
        }
        uint v2 = (v * v) % MOD;
        uint k = (v2 - v + 1) % MOD;
        uint ans = 1;
        for (uint i = 2; i <= n; ++i)
        {
            if (vars[i] && vars[i - 1])
                ans = (ans * k) % MOD;
            else
                ans = (ans * v2) % MOD;
        }
        uint a = knv;
        while (knv--)
            a = (a * v2) % MOD;
        if (a != 0)
            a -= v2;
        ans = (ans + a) % MOD;
        return ans;
    }
}

int main()
{
    using namespace xtex;
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    uint T = rdu();
    while (T--)
    {
        uint ans = sol();
        printf("%u\n", ans);
    }
    return 0;
}

/**
 * p(i, j) 表示令第i位取j的，前i-1条二元关系（BR）参数（ab），的个数
 * pt(i) 表示令前i位满足所有一元关系（1R）的，前i-1条二元关系（BR）参数（ab），的个数
 * 1. 若第1位确定：
 *     pt(1) = p(1, v(1)) = 1
 *     p(1, ...) = 0
 *    否则
 *     pt(1) = 1
 * 2. 遍历[2,n]位（i）
 *    若第i位确定:
 *     pt(i) = p(i, v(i)) = pt(i-1) * (v * v - v + 1)
 *     p(i, ...) = 0
 *    否则
 *     p(i, ...) = pt(i-1)
 *     pt(i) = v * pt(i-1)
 * 3. ans = pt(n)
 * 显然递推只需pt(x)，p(x)数组可省略
 * 1. ans = 1
 * 2. k = v * v - v + 1
 * 3. 遍历[2,n]位（i）
 *    若第i位确定:
 *     ans = ans * k
 *    否则
 *     ans = ans * v * v
 * 乱写补偿
 */
