#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
long long rand_int(long long l, long long r) {
  static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<long long>(l, r)(gen);
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T>& v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int mod = 1e9 + 7;
long long quick(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = ret * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ret;
}
int n, p;
vector<int> k;
long long ans;
int pos;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> p;
    k = vector<int>(n);
    for (int i = 0; i < n; i++) cin >> k[i];
    if (p == 1) {
      if (n & 1)
        print(1);
      else
        print(0);
      continue;
    }
    sort(k.begin(), k.end(), greater<int>());
    ans = 0;
    queue<int> q;
    for (auto u : k) q.push(u);
    bool suc = 0;
    while (!q.empty()) {
      int cc = q.front();
      q.pop();
      ans = (ans + quick(p, cc)) % mod;
      long long cnt = 1;
      while (!q.empty()) {
        while (cnt * p <= n && cc > q.front()) {
          cc--;
          cnt *= p;
        }
        if (cc > q.front() && cnt * p > n) {
          suc = 1;
          break;
        }
        cnt--;
        ans = (ans - quick(p, q.front()) + mod) % mod;
        q.pop();
        if (!cnt) break;
      }
      if (suc) break;
    }
    while (!q.empty()) {
      ans = (ans - quick(p, q.front()) + mod) % mod;
      q.pop();
    }
    print(ans);
  }
}
