#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e10, M = 1e9 + 7;
long long qpow(long long a, long long b) {
  if (b == 0) return 1;
  long long t = qpow(a, b / 2);
  t = t * t % M;
  if (b & 1)
    return t * a % M;
  else
    return t;
}
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    sort((v).begin(), (v).end(), greater<long long>());
    long long diff = 0, last = -1;
    bool too_big = false;
    for (long long i : v) {
      if (last != -1 && diff != 0) {
        long long mul = last - i;
        while (mul && !too_big) {
          diff *= p;
          mul--;
          if (diff >= INF) {
            diff %= M;
            too_big = true;
          }
        }
        if (too_big) diff %= M, diff = diff * qpow(p, mul) % M;
      }
      if (too_big || diff > 0)
        diff--;
      else
        diff++;
      last = i;
    }
    diff = (M + diff % M) % M;
    diff = diff * qpow(p, last) % M;
    cout << diff << endl;
  }
}
