#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
constexpr int inf = 1000000010;
constexpr long long INF = 1000000000000000010;
constexpr long double eps = 1e-12;
constexpr long double pi = 3.141592653589793238;
template <class T, class U>
inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U>
inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
constexpr long long mod = 1000000007;
constexpr long long modsize = 0;
vector<long long> fac(modsize);
vector<long long> inv(modsize);
vector<long long> facinv(modsize);
void modcalc() {
  if (modsize == 0) abort();
  fac[0] = 1;
  fac[1] = 1;
  inv[1] = 1;
  facinv[0] = 1;
  facinv[1] = 1;
  for (long long i = 2; i < modsize; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = mod - inv[mod % i] * (mod / i) % mod;
    facinv[i] = facinv[i - 1] * inv[i] % mod;
  }
}
long long modinv(long long a) {
  if (a == 0) abort();
  long long b = mod, u = 1, v = 0;
  while (b) {
    long long t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= mod;
  if (u < 0) u += mod;
  return u;
}
long long modpow(long long a, long long b) {
  long long ans = 1;
  a %= mod;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
long long modcomb(long long n, long long k) {
  if (n < 0 || k < 0 || n < k) return 0;
  return fac[n] * facinv[k] % mod * facinv[n - k] % mod;
}
long long modperm(long long n, long long k) {
  if (n < 0 || k < 0 || n < k) return 0;
  return fac[n] * facinv[n - k] % mod;
}
long long modhom(long long n, long long k) {
  if (n < 0 || k < 0 || n == 0 && k > 0) return 0;
  if (n == 0 && k == 0) return 1;
  return fac[n + k - 1] * facinv[k] % mod * facinv[n - 1] % mod;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    vector<long long> a(n);
    for (long long i = 0; i < (n); i++) cin >> a[i];
    if (p == 1) {
      cout << (n & 1 ? 1 : 0) << '\n';
      continue;
    }
    long long x = log2(n) + 1;
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    map<long long, long long> mp;
    set<long long> st;
    long long sum = 0;
    for (long long i = 0; i < (n); i++) {
      if (sum == 0) {
        mp[a[i]]++;
        st.insert(a[i]);
        sum++;
      } else {
        int d = *st.lower_bound(a[i]);
        if (d >= a[i] + x) {
          mp[a[i]]--;
          sum = inf;
        } else {
          mp[d]--;
          sum--;
          if (mp[d] == 0) st.erase(d);
          for (int j = a[i]; j < d; j++) {
            mp[j] = p - 1;
            st.insert(j);
            sum += p - 1;
          }
        }
      }
    }
    long long ans = 0;
    for (auto i : mp) {
      ans += modpow(p, i.first) * i.second % mod;
    }
    cout << (ans % mod + mod) % mod << '\n';
  }
}
