#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int power(int x, int y) {
  int res = 1;
  while (y) {
    if (y & 1) res = 1ll * res * x % mod;
    x = 1ll * x * x % mod, y >>= 1;
  }
  return res;
}
int main() {
  freopen("assign.in", "r", stdin);
  freopen("assign.out", "w", stdout);
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, m, v;
    cin >> n >> m >> v;
    map<int, int> must;
    int ans = 1;
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      if (must.count(x) && must[x] != y) {
        ans = 0;
      }
      must[x] = y;
    }
    if (must.empty()) {
      ans = 1ll * ans * power(v, 2 * n) % mod;
    } else {
      if (must.begin()->first != 1) {
        int len = must.begin()->first - 1;
        ans = 1ll * ans * power(v, 2 * len) % mod;
      }
      for (auto it = must.begin(); it != must.end(); it++) {
        if (next(it) == must.end()) {
          int len = n - it->first;
          ans = 1ll * ans * power(v, 2 * len) % mod;
        } else {
          int len = next(it)->first - it->first;
          ans = 1ll * ans *
                (power(v, 2 * len) -
                 1ll * 1 * power(v, len - 1) * (v - 1) % mod + mod) %
                mod;
        }
      }
    }
    cout << ans << '\n';
  }
}
