#include <bits/stdc++.h>
using namespace std;
const int N = 100000 + 10, mod = 1e9 + 7;
long long qpow(long long n, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) res = res * n % mod;
    n = n * n % mod;
    k >>= 1;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(0);
  int T;
  cin >> T;
  while (T--) {
    int n, p;
    cin >> n >> p;
    map<int, int> cnt;
    for (int i = 1, k; i <= n; i++) cin >> k, cnt[k]++;
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    vector<pair<int, int> > res;
    map<int, int> num;
    while (!cnt.empty()) {
      int x = (*cnt.begin()).first, y = cnt[x];
      cnt.erase(cnt.find(x));
      if (y / p >= 1) cnt[x + 1] += y / p, num[x + 1] = y / p;
      y %= p;
      res.push_back({x, y});
    }
    while (res.size()) {
      int x = res.back().first, y = res.back().second;
      res.pop_back();
      num[x] = min(num[x], y);
      if (y % 2 == 1 && num[x] == 0) {
        res.push_back({x, y});
        break;
      }
      int huan = num[x];
      if ((y - huan) % 2 == 1) huan--;
      if (!res.empty()) res.back().second += huan * p;
    }
    if (res.empty()) {
      cout << 0 << endl;
      continue;
    }
    long long ans = qpow(p, res.back().first);
    for (int i = 0; i < (int)res.size() - 1; i++) {
      ans = ans + mod - qpow(p, res[i].first) * res[i].second % mod;
    }
    ans %= mod;
    cout << ans << endl;
  }
}
