#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, p;
    cin >> n >> p;
    vector<int> v(n);
    for (int i = 0; i < n; ++i) cin >> v[i];
    sort(v.begin(), v.end(), greater<int>());
    if (p == 1) {
      cout << (n & 1) << '\n';
      continue;
    }
    long long nec = -1;
    vector<pair<int, int>> cur;
    function<void(int)> ins = [&](int a) {
      if (cur.empty())
        cur.emplace_back(a, 1);
      else {
        if (cur[cur.size() - 1].first == a) {
          int cur_ct = cur[cur.size() - 1].second;
          if (cur_ct == p - 1) {
            cur.pop_back();
            ins(a + 1);
          } else
            ++cur[cur.size() - 1].second;
        } else {
          cur.emplace_back(a, 1);
        }
      }
    };
    for (int a : v) {
      if (nec == -1) {
        nec = a;
      } else {
        ins(a);
      }
      if (!cur.empty()) {
        if (cur[cur.size() - 1].first == nec) {
          nec = -1;
          cur.clear();
        }
      }
    }
    if (nec == -1) {
      cout << 0 << '\n';
      continue;
    }
    long long ans = powmod(p, nec);
    for (auto a : cur) {
      ans = ((ans - powmod(p, a.first) * a.second % mod) % mod + mod) % mod;
    }
    cout << (ans + (long long)mod * mod) % mod << endl;
  }
}
