#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = (int)1e9 + 7;
struct modar {
  ll num;
  operator ll() { return num; }
};
modar operator*(modar x, modar y) { return {(x.num * y.num) % MOD}; }
modar operator+(modar x, modar y) { return {(x.num + y.num + MOD) % MOD}; }
void add_self(ll &a, ll b) {
  a += b;
  if (a > MOD) a -= MOD;
}
void sub_self(ll &a, ll b) {
  a -= b;
  if (a < 0) a += MOD;
}
void mul_self(ll &a, ll b) {
  a *= b;
  a %= MOD;
  if (a < 0) a += MOD;
}
ll power(ll base, ll exponent) {
  ll res = 1;
  base = base % MOD;
  while (exponent > 0) {
    if (exponent % 2) mul_self(res, base);
    exponent = exponent / 2;
    mul_self(base, base);
  }
  return res;
}
void solve() {
  ll n, p;
  cin >> n >> p;
  vector<int> k(n);
  for (ll i = 0; i < n; i++) {
    cin >> k[i];
  }
  if (p == 1) {
    cout << n % 2 << '\n';
    return;
  }
  sort(k.begin(), k.end());
  bool inf = false;
  ll sum = 0, cur, divided_sum = 0, prev_k = 0;
  while (!k.empty()) {
    ll latest_k = k.back();
    k.pop_back();
    if (!inf && divided_sum != 0) {
      ll exp_diff = prev_k - latest_k;
      for (ll i = 0; i < min(exp_diff, 20LL); i++) {
        divided_sum *= p;
        if (divided_sum > n) {
          inf = true;
          break;
        }
      }
    }
    cur = power(p, latest_k);
    prev_k = latest_k;
    if (inf || sum > 0) {
      sub_self(sum, cur);
      divided_sum -= 1;
    } else {
      sum = cur;
      divided_sum = 1;
    }
  }
  cout << sum << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
