#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
int n, p;
int a[N], cnt[N];
vector<int> avail;
int Pow(int y) {
  if (y == 0) return 1;
  if (y == 1) return p;
  int ret = Pow(y >> 1);
  ret = 1ll * ret * ret % mod;
  if (y & 1) ret = 1ll * ret * p % mod;
  return ret;
}
void add(int &x, const int &y) {
  x += y;
  x -= x >= mod ? mod : 0;
}
void sub(int &x, const int &y) {
  x -= y;
  x += x < 0 ? mod : 0;
}
int mul(const int &x, const int &y) { return 1ll * x * y % mod; }
void solve() {
  for (int x : avail) cnt[x] = 0;
  avail.clear();
  scanf("%d%d", &n, &p);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    cnt[a[i]]++;
    if (cnt[a[i]] == 1) avail.push_back(a[i]);
  }
  if (p == 1) {
    printf("%d\n", n & 1);
    return;
  }
  sort(avail.begin(), avail.end(), greater<int>());
  int res = 0, rem = 0;
  for (int i = 0; i < (int)(avail.size()); i++) {
    if (i && rem) {
      int fl = 0;
      for (int j = avail[i - 1]; j > avail[i]; j--) {
        if (1ll * rem * p >= 1ll * n) {
          fl = 1;
          break;
        } else
          rem *= p;
      }
      if (fl) {
        for (int j = i; j < (int)(avail.size()); j++)
          sub(res, mul(cnt[avail[j]], Pow(avail[j])));
        break;
      }
    }
    if (cnt[avail[i]] >= rem) {
      cnt[avail[i]] -= rem;
      sub(res, mul(rem, Pow(avail[i])));
      assert(res == 0);
      rem = 0;
      if (cnt[avail[i]] & 1) {
        add(res, Pow(avail[i]));
        rem = 1;
      }
    } else {
      sub(res, mul(cnt[avail[i]], Pow(avail[i])));
      rem -= cnt[avail[i]];
      cnt[avail[i]] = 0;
    }
  }
  printf("%d\n", res);
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) solve();
}
