#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1000000007;
const long long linf = 0x3f3f3f3f3f3f3f3f;
long long k[1001000];
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int ovf[1001000];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  ovf[1] = inf;
  for (int i = 2; i <= 1000000; i++) {
    long long aux = 1;
    for (int j = 0; j < 32; j++) {
      aux *= i;
      if (aux > 1e9) {
        ovf[i] = j;
        break;
      }
    }
  }
  int t_;
  cin >> t_;
  while (t_--) {
    map<long long, long long> v;
    long long n, p;
    cin >> n >> p;
    long long dif = 0, ans = 0;
    long long last = -1;
    for (int i = 1; i <= n; i++) cin >> k[i], v[k[i]]++;
    for (auto it = v.rbegin(); it != v.rend(); it++) {
      pair<int, int> q = *(it);
      long long i = q.second;
      if (dif && last != -1) {
        long long aux = power(p, last - q.first, mod);
        if (last - q.first >= ovf[p])
          dif = 1e9;
        else {
          if (dif < 1e8) dif *= aux;
        }
        ans *= aux;
        ans = ans % mod;
      }
      last = q.first;
      if (i) {
        if (dif) {
          if (i <= dif)
            dif -= i, ans -= i, i = 0;
          else
            i -= dif, dif = 0, ans = 0;
        }
        if (!dif) {
          if (i % 2) dif = 1, ans = 1;
          i = 0;
        }
      }
    }
    long long aux = power(p, last - 0, mod);
    ans *= aux;
    ans = ans % mod;
    cout << (ans + mod) % mod << "\n";
  }
  return 0;
}
