#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
inline long long modPow(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) (ret *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return ret;
}
void solve() {
  int n, p;
  cin >> n >> p;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int k;
    cin >> k;
    mp[k]++;
  }
  if (p == 1) {
    cout << (n & 1) << '\n';
    return;
  }
  vector<pair<int, int> > vec;
  for (auto x : mp) {
    vec.push_back(x);
  }
  while (!vec.empty()) {
    vec.back().second %= 2;
    if (vec.back().second == 0) {
      vec.pop_back();
      continue;
    }
    int sz = vec.size();
    long long cur = 0, rem = 1;
    long long diff = rem - cur;
    int pw = vec.back().first;
    bool flg = false;
    for (int i = sz - 2; i >= 0; i--) {
      while (pw > vec[i].first) {
        pw--;
        diff *= p;
        if (diff > 1e6) break;
      }
      if (diff > 1e6) break;
      diff -= vec[i].second;
      if (diff <= 0) {
        while (vec.size() > i + 1) vec.pop_back();
        vec.back().second = -diff;
        flg = true;
        break;
      }
    }
    if (!flg) break;
  }
  if (vec.empty()) {
    cout << 0 << '\n';
    return;
  }
  long long ans = modPow(p, vec.back().first) * vec.back().second % mod;
  for (int i = 0; i + 1 < vec.size(); i++) {
    ans += (mod - modPow(p, vec[i].first)) * vec[i].second % mod;
  }
  ans %= mod;
  cout << ans << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(32);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
