#include <bits/stdc++.h>
using namespace std;
long long int a[1000007], n, p, mod = 1e9 + 7;
long long int func(int power) {
  if (power == 0) return 1;
  if (power == 1) return p;
  long long int temp = func(power / 2);
  temp = (temp * temp) % mod;
  if (power % 2)
    return (temp * p) % mod;
  else
    return temp;
}
long long int finc(int ind) {
  long long int cur = 0, count = 0, temp;
  for (int i = ind; i >= 0; i--) {
    count = 1;
    while (i > 0 && a[i] == a[i - 1]) {
      count++;
      i--;
    }
    temp = (func(a[i]) * count) % mod;
    cur += temp;
    cur %= mod;
  }
  return cur;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    long long int diff = 0, count = 0, ans = 0, powdiff, flag = 0, index = 0,
                  temp = 0;
    cin >> n >> p;
    for (int i = 0; i < n; i++) cin >> a[i];
    if (p == 1) {
      cout << n % 2 << "\n";
      continue;
    }
    sort(a, a + n);
    for (int i = n - 1; i >= 0; i--) {
      if (count == 0) {
        diff = a[i];
        count = 1;
        temp = count;
        continue;
      }
      powdiff = diff - a[i];
      while (powdiff--) {
        count *= p;
        if (count > i + 1) {
          flag = 1;
          index = i;
          break;
        }
      }
      if (flag) break;
      count--;
      diff = a[i];
      temp = count;
    }
    ans = func(diff);
    ans = (ans * temp) % mod;
    if (flag) ans -= finc(index);
    ans = (ans + mod) % mod;
    cout << ans << "\n";
  }
  return 0;
}
