#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double EPS = 1e-9;
const long long MOD = 1e9 + 7;
const int INF = 1e9;
long long addMod(long long a, long long b) { return (a + b) % MOD; }
long long mulMod(long long a, long long b) { return (a * b) % MOD; }
long long fast(long long a, long long b) {
  if (b == 0) return 1;
  long long tmp = fast(a, b / 2);
  tmp = mulMod(tmp, tmp);
  if (b % 2 == 1) tmp = mulMod(tmp, a);
  return tmp;
}
long long GCD(long long a, long long b) {
  if (a > b) return GCD(b, a);
  if (a == 0) return b;
  return GCD(b % a, a);
}
template <typename T>
T getnum() {
  int sign = 1;
  T ret = 0;
  char c;
  do {
    c = getchar();
  } while (c == ' ' || c == '\n');
  if (c == '-')
    sign = -1;
  else
    ret = c - '0';
  while (1) {
    c = getchar();
    if (c < '0' || c > '9') break;
    ret = 10 * ret + c - '0';
  }
  return sign * ret;
}
inline void ini(int& x) { x = getnum<int>(); }
const int N = 1e6;
int ar[N + 5];
int cnt[N + 5];
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ar[0] = 0;
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    for (int i = 1; i <= n; i += 1) cin >> ar[i];
    sort(ar + 1, ar + n + 1);
    if (p == 1) {
      cout << (n + 1) / 2 - n / 2 << endl;
      continue;
    }
    long long ans = fast(p, ar[n]), bef = ar[n];
    int sign = -1, idx = n - 1, maxi;
    set<int> lst;
    lst.clear();
    long long val;
    while (idx > 0) {
      val = fast(p, ar[idx]);
      maxi = ar[idx];
      cnt[ar[idx]]++;
      lst.insert(ar[idx]);
      while (idx > 0 && maxi < bef) {
        idx--;
        if (idx == 0) break;
        cnt[ar[idx]]++;
        val = addMod(val, fast(p, ar[idx]));
        lst.insert(ar[idx]);
        if (cnt[ar[idx]] >= p) {
          int cur = ar[idx];
          while (1) {
            lst.insert(cur);
            int temp = cnt[cur] / p;
            cnt[cur] -= temp * p;
            cnt[cur + 1] += temp;
            cur++;
            if (cnt[cur] > 0) maxi = max(maxi, cur);
            if (temp == 0) break;
          }
        }
      }
      ans = addMod(ans, MOD + sign * val);
      sign *= -1;
      while (lst.size() > 0) {
        int v = *lst.begin();
        bef = -1;
        if (cnt[v] > 0) {
          if (v != maxi) bef = v;
          cnt[v] = 0;
        }
        lst.erase(v);
      }
      idx--;
      if (bef == -1 && idx > 0) {
        bef = ar[idx];
        ans = fast(p, ar[idx]);
        sign = -1;
        idx--;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
