#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 100;
const int mod = 1e9 + 7;
int f[N], p[N];
int ksm(int a, int b){
    int r = 1;
    while(b){
        if(b & 1) r = r * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return r;
}
void sol(){
    int n, m, v;cin >> n >> m >> v;
    if(v == 1){
        int ans = 1;
        for(int i = 1;i <= m;i++){
            int x, y;cin >> x >> y;
            if(y != 1) ans = 0;
        }
        cout << ans << endl;
        return ;
    }
    bool flag = 1;
    unordered_map<int, int> val;
    for(int i = 1, x;i <= m;i++){
        cin >> p[i] >> x;
        if(val.count(p[i]) && val[p[i]] != x)
            flag = 0;
        val[p[i]] = x;
    }
    if(!flag){
        cout << 0 << endl;
        return ;
    }
    sort(p + 1, p + 1 + m);
    m = unique(p + 1, p + 1 + m) - p - 1;
    int c = 2 * (p[1] - 1);
    f[1] = ksm(v, c);
    for(int i = 2;i <= m;i++){
        int k = p[i] - p[i - 1];
        if(!k) f[i] = f[i - 1] * (v * v % mod - 1) % mod;
        else f[i] = f[i - 1] * (ksm(v, 2 * k) % mod - ksm(v, k - 1) * (v - 1) % mod + mod) % mod;
    }
    cout << f[m] * ksm(v, 2 * (n - p[m])) % mod << endl;
}
int32_t main(){
    freopen("assign.in","r",stdin);
    freopen("assign.out","w",stdout);
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int cas;cin >> cas;
    while(cas--) sol();
    return 0;
}