#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const long long danhdau = 1e7;
long long a[1000004], b[1000004], p, n, T, ans;
void readInput() {}
long long powermod(long long u, long long v) {
  if (v == 0) return 1;
  if (v % 2 == 1) return (powermod(u, v - 1) * u) % mod;
  long long tmp = powermod(u, v / 2) % mod;
  return tmp * tmp % mod;
}
void solve() {
  sort(a + 1, a + n + 1);
  ans = 0;
  if (p == 1) {
    if (n % 2 == 0)
      ans = 0;
    else
      ans = 1;
  } else {
    long long res = 0;
    for (long long j = n; j >= 1; j--) {
      if (a[j] != a[j + 1]) {
        for (long long i = a[j + 1] - a[j]; i >= 1; i--) {
          res *= p;
          if (res > danhdau) break;
        }
      }
      if (res >= j) {
        for (long long i = j; i >= 1; i--) b[i] = -1;
        break;
      }
      if (res > 0) {
        res--;
        b[j] = -1;
      } else if (res == 0) {
        res++;
        b[j] = 1;
      }
    }
    for (long long j = 1; j <= n; j++) {
      if (b[j] == 1) {
        ans += powermod(p, a[j]);
        ans %= mod;
      } else {
        ans = ans + mod - powermod(p, a[j]);
        ans %= mod;
      }
    }
    ans = (ans + mod) % mod;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> T;
  while (T--) {
    cin >> n >> p;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    solve();
    cout << ans << '\n';
  }
}
