#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  int f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= f;
}
inline void write(int x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
const int N = 1000005, P = 1e9 + 7;
inline int power(int x, long long y) {
  x %= P;
  static int r;
  r = 1;
  while (y) {
    if (y & 1) r = (long long)r * x % P;
    x = (long long)x * x % P;
    y >>= 1;
  }
  return r;
}
inline long long powerc(long long x, long long y, long long k) {
  static long long r;
  r = 1;
  while (y) {
    if (y & 1) r = r * x;
    x = x * x;
    y >>= 1;
    r = min(r, k), x = min(x, k);
  }
  r = min(r, k);
  return r;
}
int n, p, a[N], b[N], ans;
inline void solve() {
  int i;
  long long now, lst, dt;
  read(n), read(p);
  for (i = 1; i <= n; ++i) read(a[i]);
  if (p == 1) {
    ans = n & 1;
    write(ans), putchar('\n');
    return;
  }
  sort(a + 1, a + n + 1);
  ans = 0;
  for (i = 1; i <= n; ++i) b[i] = power(p, a[i]);
  for (now = 0, lst = -1, i = n; i >= 1; --i) {
    if (lst == -1) {
      lst = a[i], now = 1, ans = (ans + b[i]) % P;
      continue;
    }
    if (now != 0 && lst != a[i]) {
      dt = lst - a[i];
      now *= powerc(p, dt, n);
      now = min(now, n * 1ll);
    }
    if (now) {
      --now, ans = (ans + P - b[i]) % P;
      lst = a[i];
    } else {
      ans = (ans + b[i]) % P;
      now = 1, lst = a[i];
    }
  }
  write(ans), putchar('\n');
}
int main() {
  int T;
  read(T);
  while (T--) solve();
  return 0;
}
