#include <bits/stdc++.h>
using namespace std;
long long inp, Inp;
long long i1, i2, i3, i4, i5, i6, i7, i8;
long long MOD = 998244353, MOD1 = (long long)1e9 + 7, MOD2 = (long long)1e9 + 9;
long long MOD3 = (long long)1e9 + 21;
double eps = 1e-9;
long long TMP =
    (chrono::high_resolution_clock::now().time_since_epoch().count() % MOD +
     MOD) %
    MOD;
auto STTMP = chrono::steady_clock::now();
const long long kMod = 1e9 + 7;
struct ModInt {
  long long n;
  ModInt(long long n = 0) : n(n % kMod) {}
  ModInt operator+(const ModInt& oth) { return n + oth.n; }
  ModInt operator-(const ModInt& oth) { return n - oth.n; }
  ModInt operator*(const ModInt& oth) { return n * oth.n; }
  long long get() { return n < 0 ? n + kMod : n; }
};
ModInt lgpow(ModInt b, int e) {
  ModInt r;
  for (r = 1; e; e /= 2, b = b * b)
    if (e % 2) r = r * b;
  return r;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.precision(20);
  srand((unsigned int)TMP);
  long long T;
  cin >> T;
  for (long long t = (0); t < (T); t++) {
    long long N, P;
    cin >> N >> P;
    ModInt p(P);
    vector<int> K;
    for (long long n = (0); n < (N); n++) {
      cin >> i1;
      K.push_back(i1);
    }
    sort(K.begin(), K.end());
    long long s1 = 0;
    long long s2 = 0;
    long long diff = 0;
    long long p6 = pow(P, 6);
    for (long long i = (K.size() - 1); i > (-1); i--) {
      if (i < K.size() - 1) {
        long long j = K.at(i);
        while (j < K.at(i + 1)) {
          if (P == 1 || diff == 0) {
            break;
          }
          if (diff > 1e6) {
            break;
          }
          if (K.at(i + 1) - j >= 6 && P < 20) {
            diff *= p6;
            j += 6;
            continue;
          }
          j++;
          diff *= P;
        }
      }
      if (diff == 0) {
        s1 += lgpow(p, K.at(i)).get();
        s1 %= kMod;
        diff += 1;
      } else {
        s2 += lgpow(p, K.at(i)).get();
        s2 %= kMod;
        diff -= 1;
      }
    }
    cout << (s1 - s2 + kMod) % kMod << "\n";
  }
  return 0;
}
