#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 2000005;
int T;
int n;
long long k, a[N], b[N], mu[N];
long long maxx = 0;
void Unique() {
  sort(a + 1, a + n + 1);
  int n1 = 1;
  a[n1] = a[1];
  b[n1] = 1;
  for (int i = 2; i <= n; i++) {
    if (a[i] == a[n1])
      b[n1]++;
    else {
      n1++;
      a[n1] = a[i];
      b[n1] = 1;
    }
  }
  n = n1;
}
long long calc(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b % 2) res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}
void init() {
  mu[0] = 1;
  for (int i = 1; i <= n; i++) {
    mu[i] = mu[i - 1] * k;
    if (mu[i] > 1000005) {
      maxx = i;
      break;
    }
  }
  if (maxx == 0) maxx = n;
}
void solve() {
  long long du = 0;
  long long luu = 0;
  for (int i = n; i >= 1; i--) {
    if (i != n) du *= mu[min(maxx, a[i + 1] - a[i])];
    if (du > b[i]) {
      du -= b[i];
    } else {
      int x = b[i] - du;
      du = x % 2;
      luu = du * i;
    }
    if (du > 1000005) break;
  }
  if (luu == 0)
    cout << 0 << "\n";
  else {
    long long res = calc(k, a[luu]), sum = 0;
    for (int i = luu - 1; i >= 1; i--) {
      long long tinh = b[i] * calc(k, a[i]) % MOD;
      sum = (sum + tinh) % MOD;
    }
    res -= sum;
    if (res < 0) res += MOD;
    cout << res << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    maxx = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    init();
    Unique();
    solve();
  }
  return 0;
}
