#include <bits/stdc++.h>
using namespace std;
static const int64_t kRem = 1000000007;
bool is_minus_mode;
bool change_minus_mode;
int64_t pow_rem(int64_t x, int64_t m, int64_t rem) {
  if (m == 0) {
    return 1;
  }
  if (m == 1) {
    return x;
  }
  if (m & 1) {
    int64_t val = pow_rem(x, m / 2, rem);
    val = val * val;
    if (val >= rem) {
      change_minus_mode = true;
    }
    val %= rem;
    val *= x;
    if (val >= rem) {
      change_minus_mode = true;
    }
    val %= rem;
    return val;
  } else {
    int64_t val = pow_rem(x, m / 2, rem);
    val = val * val;
    if (val >= rem) {
      change_minus_mode = true;
    }
    val %= rem;
    return val;
  }
}
void solve() {
  is_minus_mode = false;
  int64_t n, p;
  cin >> n >> p;
  vector<int64_t> ks(n);
  for (auto& k : ks) {
    cin >> k;
  }
  sort(ks.begin(), ks.end(), std::greater<int64_t>());
  int64_t ans = 1;
  int64_t prev_k = ks[0];
  for (int i = (int)(1); i < (int)(n); i++) {
    change_minus_mode = false;
    int64_t pow = pow_rem(p, prev_k - ks[i], kRem);
    if (ans != 0 && change_minus_mode) {
      is_minus_mode = true;
    }
    prev_k = ks[i];
    ans *= pow;
    if (ans >= kRem) is_minus_mode = true;
    ans %= kRem;
    if (is_minus_mode) {
      ans--;
      if (ans < 0) ans += kRem;
    } else {
      if (ans != 0)
        ans--;
      else
        ans = 1;
    }
  }
  int64_t pow = pow_rem(p, prev_k, kRem);
  ans *= pow;
  ans %= kRem;
  cout << ans << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout << fixed << setprecision(20);
  int cases;
  cin >> cases;
  for (int repeat_index = 0; repeat_index < (int)cases; repeat_index++) {
    solve();
  }
  return 0;
}
