#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9;
const int MX = 1e6 + 5;
const int MD = 1e9 + 7;
int next(vector<int> &a, int pos, long long p) {
  long long cnt = 2;
  int l = pos, r = pos;
  while (r < ((int)a.size())) {
    while (r < ((int)a.size()) && cnt > 0 && a[r] == a[l]) r++, cnt--;
    if (cnt == 0) return r;
    if (r == ((int)a.size())) return -1;
    for (int i = 0; cnt <= ((int)a.size()) && i < a[l] - a[r]; i++) cnt *= p;
    if (cnt > ((int)a.size())) return -1;
    l = r;
  }
  return -1;
}
long long pt(long long b, long long e) {
  long long ans = 1;
  while (e > 0) {
    if (e & 1) ans = (ans * b) % MD;
    b = (b * b) % MD;
    e >>= 1;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    vector<int> a(n);
    for (int &x : a) cin >> x;
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    int last = -1;
    for (int l = 0, r = 0; l < n; l = r) {
      r = next(a, l, p);
      if (r == -1) {
        last = l;
        break;
      }
    }
    if (last == -1) {
      cout << 0 << '\n';
      continue;
    }
    long long ans = pt(p, a[last]);
    for (int i = last + 1; i < n; i++) ans = (ans - pt(p, a[i]) + MD) % MD;
    cout << ans << '\n';
  }
  return 0;
}
