#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int m = 1e9 + 7;
int bin_pow(int x, int k) {
  if (k == 0) return 1;
  int g = bin_pow(x, k / 2);
  g = (1ll * g * g) % m;
  if (k % 2 > 0) g = (1ll * g * x) % m;
  return g;
}
signed main() {
  iostream::sync_with_stdio(0);
  cin.tie(0);
  setlocale(LC_ALL, "Russian");
  int tt, n, p, i, j, r, second, ans, add;
  vector<int> read, k, cnt, st;
  cin >> tt;
  for (; tt > 0; tt--) {
    cin >> n >> p;
    read.assign(n, 0);
    k.clear();
    cnt.clear();
    st.clear();
    for (i = 0; i < n; i++) cin >> read[i];
    if (p == 1) {
      if (n % 2 > 0)
        cout << "1\n";
      else
        cout << "0\n";
      continue;
    }
    sort(read.begin(), read.end());
    for (i = 0; i < n; i++) {
      j = i + 1;
      while (j < n && read[i] == read[j]) j++;
      k.push_back(read[i]);
      cnt.push_back(j - i);
      i = j - 1;
    }
    st.push_back(bin_pow(p, k[0]));
    for (i = 1; i < k.size(); i++)
      st.push_back((1ll * st[i - 1] * bin_pow(p, k[i] - k[i - 1])) % m);
    ans = 0;
    for (i = k.size() - 1; i >= 0; i--) {
      if (cnt[i] % 2 == 0) continue;
      j = i - 1;
      second = 1;
      while (j >= 0) {
        for (r = 0; r < k[j + 1] - k[j] && second <= n; r++) second *= p;
        if (r < k[j + 1] - k[j] || second > n) break;
        add = min(second, cnt[j]);
        second -= add;
        ans = (ans - 1ll * cnt[j] * st[j] + 2ll * m * m) % m;
        cnt[j] -= add;
        if (second == 0) break;
        j--;
      }
      if (second > 0) {
        while (j >= 0) {
          ans = (ans - 1ll * cnt[j] * st[j] + 2ll * m * m) % m;
          j--;
        }
        ans = (ans + st[i]) % m;
        break;
      }
      ans = 0;
      i = j + 1;
    }
    cout << ans << "\n";
  }
  return 0;
}
