#include <bits/stdc++.h>
using namespace std;
const long long INF = LLONG_MAX / 2;
const int maxn = 1e6 + 1e5;
const int MOD = 1e9 + 7;
long long abs(long long a, long long b) {
  if (a > b) return a - b;
  return b - a;
}
long long min(long long a, long long b) {
  if (a > b) return b;
  return a;
}
long long pow_mod(long long p, long long e) {
  long long ret = 1;
  while (e > 0) {
    if (e & 1) ret = ret * p % MOD;
    p = p * p % MOD;
    e /= 2;
  }
  return ret;
}
long long p_ans(vector<long long> ans, long long p) {
  long long e = ans[0], num = ans[1];
  long long r = pow_mod(p, e);
  return r * num % MOD;
}
void run() {
  long long n, p;
  cin >> n >> p;
  vector<long long> k(n);
  bool flag = false;
  for (int i = 0; i < n; i++) cin >> k[i];
  sort(k.begin(), k.end(), greater<int>());
  vector<long long> ans = {0, 0, 0};
  for (long long i = 0; i < n; i++) {
    long long e = ans[0];
    long long num = ans[1];
    long long real = ans[2];
    long long ki = k[i];
    long long delta = e - ki;
    if (flag) {
      num = pow_mod(p, delta) * num % MOD;
      ans = {ki, (num + MOD - 1) % MOD, real};
      continue;
    }
    if (real == 0) {
      ans = {ki, (num + 1) % MOD, 1};
      continue;
    }
    if (ki == e) {
      ans = {ki, (num + MOD - 1) % MOD, real - 1};
      continue;
    }
    for (long long j = 0; j < min(20, delta); j++) {
      real *= p;
      if (real >= n) flag = true;
    }
    num = pow_mod(p, delta) * num % MOD;
    ans = {ki, (num + MOD - 1) % MOD, real - 1};
  }
  cout << p_ans(ans, p) << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) run();
  return 0;
}
