#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
const long long BigN = 1000000007;
long long modpow(long long a, long long n) {
  long long ret = 1LL;
  for (long long i = 0; i < 30; i++) {
    if (n & (1 << i)) ret *= a;
    a = a * a;
    a %= BigN;
    ret %= BigN;
  }
  return ret;
}
int main(int argc, char *argv[]) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    long long N, P;
    cin >> N >> P;
    for (int i = 0; i < N; i++) {
      int t;
      cin >> t;
      mp[t]++;
    }
    long long diff = 0;
    vector<int> AV, BV;
    int last = -1;
    for (auto it = mp.rbegin(); it != mp.rend(); ++it) {
      if (last == -1) last = it->first;
      for (int i = 0; i < last - (it->first); i++) {
        if (diff == 0 || P == 1) break;
        if (diff * P > 1000000) {
          diff = 1000001;
          break;
        } else
          diff *= P;
      }
      last = it->first;
      int cnt = it->second;
      while (cnt > 0 && diff > 0) {
        AV.push_back(it->first);
        diff--;
        cnt--;
      }
      if (cnt > 0 && diff == 0) {
        for (int i = 0; i < cnt / 2; i++) {
          AV.push_back(it->first);
        }
        for (int i = 0; i < (cnt + 1) / 2; i++) {
          BV.push_back(it->first);
        }
        diff = cnt % 2;
      }
    }
    long long ans = 0;
    for (int x : BV) {
      ans += modpow(P, x);
      ans %= BigN;
    }
    for (int x : AV) {
      ans += BigN - modpow(P, x);
      ans %= BigN;
    }
    cout << ans << '\n';
    mp.clear();
  }
}
