#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e6 + 10;
const long long mod = 1e9 + 7;
long long n, t, p, k[maxn];
long long poww(long long a, long long b) {
  if (b == 0) return 1;
  long long x = poww(a, b / 2) % mod;
  if (b & 1)
    return (((x * x) % mod) * a) % mod;
  else
    return (x * x) % mod;
}
long long poww2(long long a, long long b) {
  if (b == 0) return 1;
  long long x = poww(a, b / 2);
  if (b & 1)
    return (x * x) * a;
  else
    return (x * x);
}
signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (long long i = 1; i <= n; i++) cin >> k[i];
    sort(k + 1, k + n + 1);
    if (p == 1) {
      cout << n % 2 << "\n";
      continue;
    }
    long long maxb = -1;
    for (long long i = 1; i <= n; i *= p) maxb++;
    long long ans = 0, va = 0;
    k[n + 1] = k[n];
    for (long long i = n; i >= 1; i--) {
      long long dis = k[i + 1] - k[i];
      if ((dis > maxb || (poww2(p, dis) * va) > n) && va > 0) {
        for (long long j = i; j >= 1; j--)
          ans = (ans - (poww(p, k[j]) % mod) + mod) % mod;
        break;
      }
      long long x = poww2(p, dis) % mod;
      if (va == 0) {
        va = (va * x) + 1;
        ans = (ans + poww(p, k[i]) % mod) % mod;
      } else {
        va = (va * x) - 1;
        ans = (ans - (poww(p, k[i]) % mod) + mod) % mod;
      }
    }
    cout << ans % mod << "\n";
  }
  return 0;
}
