#include <cstdio>
#include <cstring>
#include <cmath>
#include <cassert>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <random>
#define fi first
#define se second
using namespace std;
typedef long long LL;

template <typename Tp> inline void read(Tp& x) {
    char ch; bool op = 0; x = 0;
    do ch = getchar(), op |= ch == '-'; while (ch < '0' || ch > '9');
    do x = (x<<3)+(x<<1)+(ch&15), ch = getchar(); while (ch >= '0' && ch <= '9');
    if (op) x = -x;
}

template <typename Tp> inline void write(Tp x, char ed='\n') {
    static int arr[50]; int tp = 0;
    if (x < 0) putchar('-'), x = -x;
    do arr[++tp] = x % 10, x /= 10; while (x);
    for (; tp; --tp) putchar(arr[tp] | 48);
    putchar(ed);
}

const int mod = 1e9+7;
inline int fplus(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline int Fplus(int& x, int y) { return x = fplus(x, y); }
inline int fminus(int x, int y) { return x - y < 0 ? x - y + mod : x - y; }
inline int Fminus(int& x, int y) { return x = fminus(x, y); }
inline int qpow(int x, int y) {
    int res = 1;
    for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) res = 1ll * res * x % mod;
    return res;
}

bool Mst;

const int MAXN = 1e5+5;
int n, m, V, pos[MAXN];
int f[MAXN];


map<int, int> mp;

struct mat {
    int f[2][2];
    int& operator()(const int& x, const int& y) { return f[x][y]; }
    int operator()(const int& x, const int& y) const { return f[x][y]; }
    mat() {
        f[0][0] = f[1][1] = 1, f[0][1] = f[1][0] = 0;
    }
    friend mat operator*(const mat& a, const mat& b) {
        mat res;
        res(0, 0) = (1ll * a(0, 0) * b(0, 0) + 1ll * a(0, 1) * b(1, 0)) % mod;
        res(0, 1) = (1ll * a(0, 0) * b(0, 1) + 1ll * a(0, 1) * b(1, 1)) % mod;
        res(1, 0) = (1ll * a(1, 0) * b(0, 0) + 1ll * a(1, 1) * b(1, 0)) % mod;
        res(1, 1) = (1ll * a(1, 0) * b(0, 1) + 1ll * a(1, 1) * b(1, 1)) % mod;
        return res;
    }
    mat pow(int k) {
        mat res, x = *this;
        for (; k; k>>=1, x = x * x) if (k & 1) res = res * x;
        return res;
    }
};

int solve(bool tp) {
    mat mt0, mt1, res;
    mt0(0, 0) = V; 
    mt0(0, 1) = 1ll * (V-1) * V % mod;
    mt0(1, 0) = 0; 
    mt0(1, 1) = 1ll * V * V % mod;
    mt1(0, 0) = (1ll * (V-1) * V + 1) % mod;
    mt1(0, 1) = 0;
    mt1(1, 0) = 1ll * V * V % mod;
    mt1(1, 1) = 0;
    res(1, 0) = res(1, 1) = 0;
    if (tp) res(0, 0) = 1, res(0, 1) = 0;
    else res(0, 0) = 0, res(0, 1) = 1;
    int lst = 1;
    for (int i = 1; i <= m; ++i) {
        res = res * mt0.pow(pos[i] - lst - 1) * mt1;
        lst = pos[i];
    }
    res = res * mt0.pow(n - lst);
    return fplus(res(0, 0), res(0, 1));
}

bool Med;


int main() {
    freopen("assign.in", "r", stdin);
    freopen("assign.out", "w", stdout);
    fprintf(stderr, "%.3lf\n", (&Mst - &Med) / 1048576.);
    int T; read(T);
    while (T--) {
        read(n), read(m), read(V);
        bool fl = 1, tp = 0;
        mp.clear();
        for (int i = 1, x; i <= m; ++i) {
            read(pos[i]), read(x);
            auto it = mp.find(pos[i]);
            if (it == mp.end()) mp[pos[i]] = x;
            else {
                if (it->se != x) { fl = 0; break; }
                --i, --m;
            }
            if (pos[i] == 1) tp = 1, --i, --m;
        }
        if (!fl) {
            puts("0");
            continue;
        }
//        puts("BEGIN");
        sort(pos+1, pos+m+1);
        write(solve(tp));
    }
    return 0;
}

