#include <cstdio>
#include <map>
#define int long long
#define mod 1000000007
using namespace std;
map<int, int> mp;
struct matrix {
    int a[2][2];
    inline matrix operator*(const matrix& x) const {
        matrix ans;
        ans.a[0][0] = (a[0][0] * x.a[0][0] + a[0][1] * x.a[1][0]) % mod;
        ans.a[0][1] = (a[0][0] * x.a[0][1] + a[0][1] * x.a[1][1]) % mod;
        ans.a[1][0] = (a[1][0] * x.a[0][0] + a[1][1] * x.a[1][0]) % mod;
        ans.a[1][1] = (a[1][0] * x.a[0][1] + a[1][1] * x.a[1][1]) % mod;
        return ans;
    }
};
matrix qpow(matrix a, int b) {
    matrix ans;
    ans.a[0][0] = ans.a[1][1] = 1;
    ans.a[0][1] = ans.a[1][0] = 0;
    while (b) {
        if (b & 1)
            ans = ans * a;
        a = a * a;
        b >>= 1;
    }
    return ans;
}
signed main() {
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    int T;
    scanf("%lld", &T);
    while (T--) {
        mp.clear();
        int n, m, v;
        scanf("%lld%lld%lld", &n, &m, &v);
        const int V = v * v - v + 1;
        bool pd = 0;
        while (m--) {
            int c, d;
            scanf("%lld%lld", &c, &d);
            if (mp[c] && mp[c] != d)
                pd = 1;
            mp[c] = d;
        }
        if (pd) {
            puts("0");
            continue;
        }
        matrix a, ans;
        a.a[0][0] = v * v % mod;
        a.a[0][1] = 0;
        a.a[1][0] = (v * v - v) % mod;
        a.a[1][1] = v % mod;
        ans.a[1][0] = ans.a[1][1] = 0;
        if (mp[1]) {
            ans.a[0][0] = 0;
            ans.a[0][1] = 1;
        } else {
            ans.a[0][0] = 1;
            ans.a[0][1] = 0;
        }
        int f = 1, s = 0;
        mp[1] = 1;
        for (auto it = mp.begin(); ; ) {
            auto i = *it;
            if ((++it) == mp.end()) {
                ans = ans * qpow(a, n - i.first);
                break;
            }
            ans = ans * qpow(a, it -> first - i.first - 1);
            int f = ((v * v - v) % mod * ans.a[0][1] % mod + v * v % mod * ans.a[0][0] % mod) % mod;
            int s = ans.a[0][1] * (mp[it -> first] ? 1 : v) % mod;
            if (mp[it -> first]) {
                s += f;
                f = 0;
            }
            ans.a[0][0] = f;
            ans.a[0][1] = s;
        }
        printf("%lld\n", (ans.a[0][0] + ans.a[0][1]) % mod);
    }
    return 0;
}