#include <bits/stdc++.h>
using namespace std;
int read() {
  int x = 0;
  bool flg = false;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') flg = true;
  for (; isdigit(ch); ch = getchar()) x = (x << 3) + (x << 1) + (ch ^ 48);
  return flg ? -x : x;
}
const int mod = 1e9 + 7;
inline int upd(const int &x) { return x + (x >> 31 & mod); }
inline void add(int &x, const int &y) { x = upd(x + y - mod); }
inline void iadd(int &x, const int &y) { x = upd(x - y); }
int qpow(int x, int y) {
  int res = 1;
  for (; y; y >>= 1, x = 1LL * x * x % mod)
    if (y & 1) res = 1LL * res * x % mod;
  return res;
}
int n, p;
int w[1000010];
void solve() {
  n = read(), p = read();
  for (int i(1); i <= int(n); i++) w[i] = read();
  if (p == 1) return printf("%d\n", n & 1), void();
  sort(w + 1, w + n + 1);
  while (n) {
    while (n >= 2 && w[n] == w[n - 1]) n -= 2;
    if (!n) break;
    long long tot = 1;
    int C = n - 1;
    bool f = 1;
    while (C && tot) {
      for (int i(w[C] + 1); i <= int(w[C + 1]); i++) {
        tot *= p;
        if (tot > n) {
          f = 0;
          break;
        }
      }
      if (!f) break;
      tot--;
      C--;
    }
    if (f && !tot) {
      n = C;
      continue;
    }
    int ans = qpow(p, w[n]);
    for (int i(1); i <= int(n - 1); i++) iadd(ans, qpow(p, w[i]));
    printf("%d\n", ans);
    return;
  }
  puts("0");
}
int main() {
  for (int T = read(); T--;) solve();
  return 0;
}
