#include <bits/stdc++.h>
using namespace std;
const bool debug = 0;
long long int modPow(long long int b, int x) {
  long long int res = 1;
  while (x > 0) {
    if (x & 1) res = (res * b) % 1000000007;
    b = (b * b) % 1000000007;
    x >>= 1;
  }
  return res;
}
int calc(long long int need, int base, int inc, int sz) {
  while (inc-- && need <= 1e6 + 5) {
    need *= base;
  }
  if (need > 1e6 + 5)
    return -1;
  else
    return need;
}
long long int solve() {
  int n, p;
  cin >> n >> p;
  vector<int> a(n);
  for (int &x : a) cin >> x;
  if (p == 1) {
    return n % 2;
  }
  sort(a.rbegin(), a.rend());
  long long int ans = 0;
  vector<pair<int, int> > fr;
  for (int i = 0; i < n; ++i) {
    int cnt = 0;
    while (i + cnt < n && a[i + cnt] == a[i]) cnt++;
    fr.push_back({cnt, a[i]});
    i += cnt - 1;
  }
  if (debug)
    for (auto x : fr) cout << x.first << " " << x.second << '\n';
  n = fr.size();
  for (int i = 0; i < n; ++i) {
    if (fr[i].first & 1) {
      int need = 1;
      int l = fr[i].second, even = 0;
      for (int j = i + 1; j < n; ++j) {
        int new_need = calc(need, p, l - fr[j].second, n - j);
        if (new_need == -1) break;
        if (new_need <= fr[j].first) {
          fr[j].first -= new_need;
          i = j - 1;
          even = 1;
          break;
        } else {
          new_need -= fr[j].first;
          need = new_need;
          l = fr[j].second;
        }
      }
      if (!even) {
        ans = (ans + modPow(p, fr[i].second)) % 1000000007;
        for (int j = i + 1; j < n; ++j) {
          long long int val =
              (fr[j].first * modPow(p, fr[j].second)) % 1000000007;
          ans = (ans + 1000000007 - val) % 1000000007;
        }
        break;
      }
    }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int _;
  cin >> _;
  while (_--) {
    cout << solve() << '\n';
  }
  return 0;
}
