#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VI = vector<int>;
using VLL = vector<LL>;
using VPI = vector<pair<int, int>>;
using UMii = unordered_map<int, int>;
void SC(int &i) { scanf("%d", &i); }
void SC(int &i, int &j) { scanf("%d %d", &i, &j); }
void SC(int &i, int &j, int &k) { scanf("%d %d %d", &i, &j, &k); }
void SC(LL &i) { scanf("%lld", &i); }
void SC(LL &i, LL &j) { scanf("%lld %lld", &i, &j); }
void SC(LL &i, LL &j, LL &k) { scanf("%lld %lld %lld", &i, &j, &k); }
const LL INF = 1e9;
const LL MOD = 1e9 + 7;
LL power(LL a, LL b) {
  LL ans = 1;
  LL c = a;
  while (b) {
    if (b & 1) {
      ans *= c;
      ans %= MOD;
    }
    c *= c;
    c %= MOD;
    b >>= 1;
  }
  return ans;
}
int main() {
  int T;
  SC(T);
  while (T--) {
    LL p = 0, n = 0;
    SC(n, p);
    VLL a;
    LL temp = 0;
    for (int i = 0; i < n; ++i) {
      SC(temp);
      a.push_back(temp);
    }
    sort(a.begin(), a.end(), greater<LL>());
    LL ind = a[0];
    LL con = 1;
    bool allSmall = false;
    LL i = 1;
    if (p == 1) {
      if (n % 2)
        cout << "1" << endl;
      else
        cout << "0" << endl;
      continue;
    }
    while (i < n and (not allSmall)) {
      if (con == 0) {
        ind = a[i];
        con = 1;
        i += 1;
      } else {
        while (ind > a[i]) {
          con *= p;
          ind -= 1;
          if (con > n) {
            allSmall = true;
            con = ((con % MOD) * power(p, ind - a[i])) % MOD;
            ind = a[i];
            if (not con) con = MOD;
          }
        }
        con -= 1;
        i += 1;
      }
    }
    if (allSmall) {
      while (i < n) {
        if (not con) con = MOD;
        con = ((con % MOD) * power(p, ind - a[i])) % MOD;
        ind = a[i];
        if (not con) con = MOD;
        con -= 1;
        i += 1;
      }
    }
    cout << (((con % MOD) * power(p, ind)) % MOD) << endl;
  }
  return 0;
}
