#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e6 + 10;
const long long INF = 2e9;
const long long mod = 1e9 + 7;
long long T, ans, n, p, k[MAXN];
template <typename T>
inline void read(T &a) {
  a = 0;
  char c = getchar();
  long long f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = a * 10 + (c ^ 48);
    c = getchar();
  }
  a *= f;
}
inline long long ksm(long long x, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) (res *= x) %= mod;
    (x *= x) %= mod;
    b >>= 1;
  }
  return res;
}
signed main() {
  read(T);
  while (T--) {
    read(n);
    read(p);
    for (register long long i = 1; i <= n; ++i) read(k[i]);
    sort(k + 1, k + n + 1);
    long long maxcnt = 0, maxk = 0;
    ans = 0;
    for (register long long i = n; i >= 1; --i) {
      if (!maxcnt) {
        maxk = k[i], maxcnt = 1;
        continue;
      }
      if (maxk == k[i]) {
        maxcnt--;
        continue;
      }
      long long tmp = maxcnt, x = p, b = k[i + 1] - k[i];
      while (b) {
        if (x > i) {
          tmp = x;
          break;
        }
        if (b & 1) tmp *= x;
        x *= x;
        b >>= 1;
        if (tmp > i) break;
      }
      if (tmp > i) {
        for (register long long j = i; j >= 1; --j)
          (ans -= ksm(p, k[j])) %= mod;
        break;
      } else
        maxcnt = tmp - 1, maxk = k[i];
    }
    ans = ((ans + maxcnt * ksm(p, maxk) % mod) % mod + mod) % mod;
    cout << ans << endl;
  }
  return 0;
}
