#include <bits/stdc++.h>
using namespace std;
long long n, a[1000006], f[1000006], p;
long long pwr(long long b, long long e) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % 1000000007ll;
    e >>= 1;
    b = (b * b) % 1000000007ll;
  }
  return ans;
}
long long solve(long long extras, long long requiredcnt) {
  long long picked = 1;
  for (long long i = 1; i <= requiredcnt; ++i) {
    if (picked <= 1000000 / p)
      picked = picked * p;
    else
      return -1;
  }
  if (picked <= 1000000 / extras)
    return picked * extras;
  else
    return -1;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      if (p != 1) f[a[i]]++;
    }
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    }
    sort(a, a + n);
    reverse(a, a + n);
    long long nn = 0;
    a[nn] = a[0];
    nn++;
    for (int i = 1; i < n; ++i) {
      if (a[i] != a[i - 1]) {
        a[nn] = a[i];
        nn++;
      }
    }
    n = nn;
    long long ans = 0;
    for (int i = 0; i < n;) {
      if (!(f[a[i]] & 1)) {
        i++;
        continue;
      }
      long long balanced = 0, extra = 1, requiredcnt = a[i];
      for (int j = i + 1; j < n; ++j) {
        long long fun = solve(extra, requiredcnt - a[j]);
        if (fun == -1) break;
        if (fun <= f[a[j]]) {
          balanced = 1;
          f[a[j]] -= fun;
          i = j;
          break;
        } else {
          extra = fun - f[a[j]];
          requiredcnt = a[j];
        }
      }
      if (balanced == 0) {
        ans = (ans + pwr(p, a[i])) % 1000000007ll;
        for (int j = i + 1; j < n; ++j) {
          ans = (ans - (pwr(p, a[j]) * f[a[j]]) % 1000000007ll + 1000000007ll) %
                1000000007ll;
        }
        break;
      }
    }
    cout << ans << '\n';
    for (int i = 0; i < nn; ++i) {
      f[a[i]] = 0;
    }
  }
  return 0;
}
