#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
long long raise(long long a, int b) {
  if (a == 0)
    return 0;
  else if (b == 0)
    return 1;
  long long x = raise(a, b / 2);
  x = (x * x) % mod;
  if (b & 1) x = (x * a) % mod;
  return x;
}
void solve() {
  int n, p;
  cin >> n >> p;
  int k[n];
  int mx = -1;
  long long sum = 0;
  long long a, b, c;
  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    cin >> k[i];
    mp[k[i]]++;
    mx = max(mx, k[i]);
  }
  long long num = 0, ex = -1;
  auto it = mp.end();
  it--;
  while (true) {
    int a1 = it->first, a2 = it->second;
    double d = log2(n * 1.0);
    double d1 = log2(num * 1.0);
    d = (d - d1) / log2(p * 1.0);
    double diff = ex - a1;
    if (d < diff) {
      num = n + 1;
    } else {
      a = raise(p, ex - a1);
      num *= (int)a;
    }
    if (num > n) {
      a = raise(p, a1);
      a = (a * a2) % mod;
      sum = (sum - a + mod) % mod;
    } else {
      if (a2 <= num) {
        num = (num - a2);
        a = raise(p, a1);
        a = (a * a2) % mod;
        sum = (sum - a + mod) % mod;
      } else {
        c = (a2 - num) % 2;
        a2 = c - num;
        if (a2 < 0) a2 += (int)mod;
        num = c;
        a = raise(p, a1);
        a = (a * a2) % mod;
        sum = (sum + a) % mod;
      }
    }
    ex = it->first;
    if (it == mp.begin()) break;
    it--;
  }
  cout << sum << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
