#include <bits/stdc++.h>
using namespace std;
inline long long mod(long long n, long long m) {
  long long ret = n % m;
  if (ret < 0) ret += m;
  return ret;
}
long long gcd(long long a, long long b) {
  return (b == 0LL ? a : gcd(b, a % b));
}
long long exp(long long a, long long b, long long m) {
  if (b == 0LL) return 1LL;
  if (b == 1LL) return mod(a, m);
  long long k = mod(exp(a, b / 2, m), m);
  if (b & 1LL) {
    return mod(a * mod(k * k, m), m);
  } else
    return mod(k * k, m);
}
const long long N = 1e6 + 10;
const long long M = 1e9 + 7;
long long k[N];
void gen() {
  long long tam = 9902;
  cout << 1 << "\n";
  cout << tam << " " << 100 << "\n";
  long long big = 1000000;
  cout << big << " ";
  long long p = 100;
  tam++;
  for (long long j = 0; j < 100; j++) {
    for (long long i = 1; i <= p - 1; i++) {
      cout << big - 1 - j << " ";
      tam++;
    }
    if (j == 99) cout << big - 1 - j << "\n", tam++;
  }
}
void solve() {
  long long n;
  long long p;
  cin >> n >> p;
  for (long long i = n; i <= n + 5; i++) {
    k[i] = 0;
  }
  for (long long i = 1; i <= n; i++) {
    cin >> k[i];
  }
  sort(k + 1, k + 1 + n, greater<long long>());
  if (p == 1) {
    if (n & 1)
      cout << 1 << "\n";
    else
      cout << 0 << "\n";
    return;
  }
  long long res = 0;
  for (long long i = 1; i <= n;) {
    long long cur = k[i];
    long long ini = i;
    i++;
    long long ok = 0;
    map<long long, long long> cnt;
    while (i <= n) {
      long long pos = k[i];
      i++;
      cnt[pos]++;
      while (pos < cur and cnt[pos] == p) {
        cnt[pos] = 0;
        pos++;
        cnt[pos]++;
      }
      if (pos == cur and cnt[pos] == 1) {
        cnt[pos] = 0;
        ok = 1;
        break;
      }
    }
    if (ok) {
    } else {
      assert(i == n + 1);
      res = exp(p, cur, M);
      for (long long j = ini + 1; j < i; j++) {
        res = mod(res - exp(p, k[j], M), M);
      }
      break;
    }
  }
  res = mod(res, M);
  cout << res << "\n";
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
}
