#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#define debug(...) fprintf(stderr, ##__VA_ARGS__)
#else
#define debug(...) void(0)
#define endl "\n"
#endif
using LL = long long;
template <unsigned umod>
struct modint {/*{{{*/
  static constexpr int mod = umod;
  unsigned v;
  modint() = default;
  template <class T, enable_if_t<is_integral<T>::value>* = nullptr> modint(const T& _y):
    v((unsigned)(_y % mod + (is_signed<T>() && _y < 0 ? mod : 0))) {}
  modint& operator+=(const modint& rhs) { v += rhs.v; if (v >= umod) v -= umod; return *this; }
  modint& operator-=(const modint& rhs) { v -= rhs.v; if (v >= umod) v += umod; return *this; }
  modint& operator*=(const modint& rhs) { v = (unsigned)(1ull * v * rhs.v % umod); return *this; }
  modint& operator/=(const modint& rhs) { assert(rhs.v); return *this *= qpow(rhs, mod - 2); }
  template <class T, enable_if_t<is_integral<T>::value>* = nullptr> friend modint qpow(modint a, T b) {
    modint r = 1;
    while (b) {
      if (b & 1) r *= a;
      if (b >>= 1) a *= a;
    }
    return r;
  }
  friend int raw(modint self) { return self.v; }
  friend ostream& operator<<(ostream& os, modint self) { return os << raw(self); }
  friend modint operator+(modint lhs, const modint& rhs) { return lhs += rhs; }
  friend modint operator-(modint lhs, const modint& rhs) { return lhs -= rhs; }
  friend modint operator*(modint lhs, const modint& rhs) { return lhs *= rhs; }
  friend modint operator/(modint lhs, const modint& rhs) { return lhs /= rhs; }
  explicit operator bool() const { return v != 0; }
};/*}}}*/
using mint = modint<(int)1e9 + 7>;
int n, m, v;
int mian() {
  cin >> n >> m >> v;
  map<int, int> mp;
  bool flag = false;
  for (int i = 1, c, d; i <= m; i++) {
    cin >> c >> d;
    if (mp.count(c) && mp[c] != d) flag = true;
    else mp[c] = d;
  }
  if (flag) return cout << 0 << endl, 0;
  int lst = 0;
  mint ans = 1;
  for (auto e : mp) {
    int now = e.first;
    if (!lst) ans *= qpow(mint(v), 2 * (now - 1));
    else ans *= qpow(mint(v), 2 * (now - lst)) - qpow(mint(v), now - lst - 1) * (v - 1);
    lst = now;
  }
  ans *= qpow(mint(v), 2 * (n - lst));
  cout << ans << endl;
  return 0;
}
int main() {
#ifndef LOCAL
#ifndef NF
  freopen("assign.in", "r", stdin);
  freopen("assign.out", "w", stdout);
#endif
  cin.tie(nullptr)->sync_with_stdio(false);
#endif
  int t;
  cin >> t;
  while (t--) mian();
  return 0;
}

