#include <bits/stdc++.h>
#define IL inline
#define LL long long
#define eb emplace_back
#define sz(v) ((int) (v).size())
#define me(f, x) memset(f, x, sizeof(f))
#define mc(f, g) memcpy(f, g, sizeof(f))
#define L(i, j, k) for (int i = (j); i <= (k); ++i)
#define R(i, j, k) for (int i = (j); i >= (k); --i)
#define FIO(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
using namespace std;

using vi = vector<int>;

constexpr int N = 1e5 + 9;
constexpr int P = 1e9 + 7;
int n, m, v, a[N];
map<int, int> mp;

IL int qpow (int b, int k = P - 2) {
  int s = 1;
  for (; k; k >>= 1, b = (LL)b * b % P) if (k & 1) s = (LL)s * b % P;
  return s;
}

void slv () {
  cin >> n >> m >> v;
  int ans = 1;
  mp.clear();
  L (i, 1, m) {
    int x;
    cin >> a[i] >> x;
    if (mp.find(a[i]) != mp.end()) {
      if (mp[a[i]] != x) {
        ans = 0;
      } 
    }
    mp[a[i]] = x;
  }
  if (!ans) {
    cout << "0\n";
    return;
  }
  sort(a + 1, a + m + 1);
  m = unique(a + 1, a + m + 1) - a - 1;
  int v2 = (LL)v * v % P;
  if (a[1] > 1) {
    ans = (LL)ans * qpow(v2, a[1] - 1) % P;
  }
  if (a[m] < n) {
    ans = (LL)ans * qpow(v2, n - a[m]) % P;
  }
  L (i, 2, m) {
    int len = a[i] - a[i - 1];
    ans = (LL)ans * (qpow(v2, len) + (P - qpow(v, len - 1)) * (LL)(v - 1) % P) % P;
  }
  cout << ans << '\n';
}

int main () {
  FIO("assign");
  ios::sync_with_stdio(0), cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    slv();
  }
}
// I love WHQ!