#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldb = long double;
using ui = unsigned int;

#define mp make_pair
#define eb emplace_back
#define fi first
#define se second

#ifdef _DEBUG
#define dbg(...) __VA_ARGS__
#else
#define dbg(...) void();
#endif

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int radint(int l, int r) {
  return uniform_int_distribution<>(l, r)(rng);
}

const int N = 1e5 + 5, mod = 1e9 + 7;
ll f[N];
pair<int, int> lim[N];

ll qpow(ll a, ll b) {
  if (b < 0) return 0;
  ll res = 1;
  for (; b; b >>= 1, a = a * a % mod)
    if (b & 1) res = res * a % mod;
  return res;
}

void solve() {
  int n, m, v;
  cin >> n >> m >> v;
  for (int i = 1; i <= m; ++i) {
    cin >> lim[i].fi >> lim[i].se;
  }
  sort(lim + 1, lim + 1 + m);
  m = unique(lim + 1, lim + 1 + m) - lim - 1;
  for (int i = 2; i <= m; ++i) {
    if (lim[i].fi == lim[i - 1].fi && lim[i].se != lim[i - 1].se) {
      cout << "0\n";
      return;
    }
  }
  f[0] = 1;
  ll ans = 0;
  for (int i = 1; i <= m; ++i) {
    if (i > 1) {
      ll delta = lim[i].fi - lim[i - 1].fi;
      ll all = qpow(v, 2 * delta);
      ll bad = (v - 1) % mod * qpow(v, delta - 1) % mod;
      ll val = (all + mod - bad) % mod;
      f[i] = 1ll * f[i - 1] * val % mod;
    } else {
      f[1] = qpow(v, 2ll * (lim[i].fi - 1));
    }
    if (i < m) {
      ll delta2 = n - lim[i + 1].fi;
      ll delta3 = lim[i + 1].fi - lim[i].fi;
      ans += (mod - f[i] * qpow(v, 2 * delta2) % mod * (v - 1) % mod * qpow(v, delta3 - 1) % mod) % mod;
      ans %= mod;
    }
  }
  ans += qpow(v, 2ll * (n - 1)) % mod;
  ans %= mod;
  cout << ans << '\n';
}

int main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  freopen("assign.in", "r", stdin);
  freopen("assign.out", "w", stdout);
  int tt;
  cin >> tt;
  while (tt--) solve();
  return 0;
}