#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const long long mint = 0x7fffffff;
const long long linf = 1000000000000000000LL;
const long long mod = 1000000007LL;
const double eps = 1e-3;
const int N = 1000020;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
long long qpow(long long a, long long b, long long m) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = ans * a % m;
    }
    a = a * a % m;
    b >>= 1;
  }
  return ans;
}
int main(void) {
  for (int _ = read(); _ > 0; _--) {
    int n = read();
    long long p = read();
    vector<long long> k(n);
    long long b = 0;
    for (int i = 0; i < n; i++) k[i] = read();
    sort(k.begin(), k.end(), greater<long long>());
    long long q = 1;
    bool f = false;
    if (p == 1) {
      printf("%d\n", n % 2 == 1 ? 1 : 0);
      continue;
    }
    long long now = k[0];
    b = 1;
    int i = 1;
    for (; i < n; i++) {
      if (b == 0)
        now = k[i];
      else
        while (now > k[i] && !f) {
          if (b >= n) {
            f = true;
            break;
          }
          b = b * p;
          now--;
        }
      if (f) {
        break;
      } else {
        if (b > 0)
          b--;
        else
          b++;
      }
    }
    long long ans = b % mod * qpow(p, now, mod) % mod;
    for (; i < n; i++) ans = (ans + (mod - 1) * qpow(p, k[i], mod) % mod) % mod;
    printf("%lld\n", ans);
  }
  return 0;
}
