#include <bits/stdc++.h>
using namespace std;
long long int powermod(long long int x, long long int y, long long int m) {
  if (y == 0) return 1;
  long long int p = powermod(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
void shikhar7s(long long int cas) {
  long long int n, p, i;
  cin >> n >> p;
  long long int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  long long int x, b = 0, c, f = 0, j, d, xx;
  for (i = n - 1; i >= 0; i--) {
    c = 0;
    j = i;
    while (j >= 0 && a[j] == a[i]) {
      c++;
      j--;
    }
    if (!f && c > b) {
      c -= b;
      if (c % 2)
        b = 1;
      else
        b = 0;
    } else {
      b = (b - c + 1000000007) % 1000000007;
    }
    if (j >= 0) {
      x = a[i] - a[j];
      i = j + 1;
      if (!f) {
        xx = x;
        d = b;
        while (p != 1 && d && xx && d <= 1000000) {
          d *= p;
          xx--;
        }
        if (d > 1000000) f = 1;
      }
      x = powermod(p, x, 1000000007);
      b = (b * x) % 1000000007;
    } else
      break;
  }
  x = powermod(p, a[0], 1000000007);
  b = (b * x) % 1000000007;
  cout << b << "\n";
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int t = 1;
  cin >> t;
  long long int cas = 1;
  while (cas <= t) {
    shikhar7s(cas);
    cas++;
  }
  return 0;
}
