#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx")
bool RELAXED = false;
template <int, typename T>
struct MINMAX {
  T val;
  MINMAX(T val) : val(val) {}
};
template <typename T>
MINMAX<1, T> MAX(T val) {
  return MINMAX<1, T>(val);
};
template <typename T>
MINMAX<2, T> MIN(T val) {
  return MINMAX<2, T>(val);
};
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<1, U> rhs) {
  return lhs = (rhs.val > lhs ? (RELAXED = true, rhs.val)
                              : (RELAXED = false, lhs));
}
template <typename T, typename U>
inline T& operator|=(T& lhs, MINMAX<2, U> rhs) {
  return lhs = (rhs.val < lhs ? (RELAXED = true, rhs.val)
                              : (RELAXED = false, lhs));
}
template <typename T, typename U>
istream& operator>>(istream& in, pair<T, U>& p) {
  in >> p.first >> p.second;
  return in;
}
template <typename T>
inline vector<T> READ(int n) {
  vector<T> vec(n);
  for (int i = 0; i < int(n); i++) cin >> vec[i];
  return vec;
}
template <typename T>
using min_heap = priority_queue<T, vector<T>, greater<T> >;
const int MAXN = 1e6 + 5;
const signed long long int MOD = 1e9 + 7;
signed long long int fastpow(signed long long int x, signed long long int k) {
  signed long long int ans = 1;
  for (int b = 0; b < int(62); b++) {
    if (k & (1LL << b)) ans = ans * x % MOD;
    x = x * x % MOD;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << fixed << setprecision(12);
  int T;
  cin >> T;
  while (T--) {
    int n, p;
    cin >> n >> p;
    map<int, int> cnt;
    vector<int> ki = READ<int>(n);
    for (auto k : ki) {
      cnt[k]++;
    }
    signed long long int ans = 0;
    signed long long int diff = 0;
    int last_exp = -1;
    for (auto it = cnt.rbegin(); it != cnt.rend(); ++it) {
      int exp = it->first;
      int k = it->second;
      42;
      if (diff == 0) {
        k = k % 2;
        diff = k;
        last_exp = exp;
        ans = fastpow(p, exp) * k;
        continue;
      }
      int step_exp = last_exp - exp;
      for (int i = 0; i < int(step_exp); i++) {
        double d_diff = 1. * diff * p;
        if (d_diff > 1e18) {
          diff = 1e18;
          break;
        } else {
          diff *= p;
        }
        if (p == 1) break;
      }
      if (k >= diff) {
        k -= diff;
        diff = 0;
        k %= 2;
        diff = k;
        ans = fastpow(p, exp) * k;
      } else {
        diff -= k;
        ans = (ans - fastpow(p, exp) * k) % MOD;
        ans = (ans + MOD) % MOD;
      }
      last_exp = exp;
      42;
    }
    cout << ans << endl;
    42;
  }
}
