#include <bits/stdc++.h>
using namespace std;
const int N = 1000050;
const int inf = 0x3f3f3f3f;
const long long llinf = 0x3f3f3f3f3f3f;
const long long mod = 1000000007;
clock_t TIME_START, TIME_END;
void program_end() {}
int n;
long long p;
long long k[N];
inline long long mi(long long a, long long b, long long res, bool f) {
  if (res == 0 && !f) return 0;
  long long ret = (f) ? 1 : res;
  while (b) {
    if (b & 1) {
      ret *= a;
      if (f) ret %= mod;
    }
    if (!f && (ret > 1e6 || a > 1e6)) return -1;
    a *= a;
    if (f) a %= mod;
    b >>= 1;
  }
  return ret;
}
void solve() {
  scanf("%d%lld", &n, &p);
  for (int i = 1; i <= n; ++i) scanf("%lld", &k[i]);
  sort(k + 1, k + n + 1, greater<long long>());
  k[n + 1] = 0;
  long long cha = 0;
  for (int i = 1; i <= n; ++i) {
    while (k[i] == k[i + 1] && i < n) {
      if (cha <= 0)
        cha++;
      else
        cha--;
      ++i;
    }
    cha = cha + ((cha <= 0) ? 1 : -1);
    long long tmp = mi(p, k[i] - k[i + 1], cha, 0);
    if (tmp > 1e6) tmp = -1;
    if (tmp == -1) {
      long long ans = mi(p, k[i], 0, 1) * cha % mod;
      for (int j = i + 1; j <= n; ++j)
        ans = (ans - mi(p, k[j], 0, 1) + mod) % mod;
      return printf("%lld\n", ans), void();
    }
    cha = tmp;
  }
  printf("%lld\n", cha);
}
int main() {
  TIME_START = clock();
  int Test = 1;
  cin >> Test;
  while (Test--) solve();
  TIME_END = clock();
  program_end();
  return 0;
}
