#include <bits/stdc++.h>
using namespace std;
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
double PI = 3.141592653589793;
const int P = 1e9 + 7;
class mint {
 public:
  static int init_siz;
  static vector<mint> inverse, factorial;
  static int mintP;
  int n;
  static mint exp(mint a, long long e) {
    mint res = 1;
    while (e) {
      if (e & 1) res *= a;
      a *= a;
      e >>= 1;
    }
    return res;
  }
  static void init(int n) {
    init_siz = n;
    inverse.resize(n + 1);
    factorial.resize(n + 1);
    factorial[0] = 1;
    for (int i = 1; i <= n; i++) factorial[i] = factorial[i - 1] * i;
    inverse[n] = exp(factorial[n], mintP - 2);
    for (int i = n - 1; i >= 0; i--) inverse[i] = inverse[i + 1] * (i + 1);
  }
  static mint inv(mint const& a) {
    return (init_siz >= a.n ? inverse[a.n] * (a.n > 1 ? fac(a - 1) : 1)
                            : exp(a, mintP - 2));
  }
  static mint cinv(mint const& a) {
    return (init_siz >= a.n ? inverse[a.n] : exp(fac(a), mintP - 2));
  }
  static mint fac(mint const& a) {
    if (init_siz >= a.n)
      return factorial[a.n];
    else {
      mint out = 1;
      for (int i = 0; i < a.n; i++) out *= (i + 1);
      return out;
    }
  }
  mint(long long val = 0) {
    n = val % mintP;
    if (n < 0) n += mintP;
  }
  mint& operator+=(mint const& b) {
    n += b.n;
    if (n >= mintP) n -= mintP;
    return *this;
  }
  mint& operator-=(mint const& b) {
    n -= b.n;
    if (n < 0) n += mintP;
    return *this;
  }
  mint& operator*=(mint const& b) {
    n = (long long)n * b.n % mintP;
    return *this;
  }
  mint exp(long long e) { return exp(*this, e); }
  mint inv() { return inv(*this); }
  mint& operator/=(mint const& b) { return *this *= inv(b); }
  friend std::ostream& operator<<(std::ostream& os, mint const& a) {
    return os << (a.n < 0 ? a.n + mintP : a.n);
  }
  friend mint operator+(mint a, mint const b) { return a += b; }
  friend mint operator-(mint a, mint const b) { return a -= b; }
  friend mint operator-(mint const a) { return 0 - a; }
  friend mint operator*(mint a, mint const b) { return a *= b; }
  friend mint operator/(mint a, mint const b) { return a /= b; }
  friend bool operator==(mint const& a, mint const& b) { return a.n == b.n; }
  friend bool operator!=(mint const& a, mint const& b) { return a.n != b.n; }
};
int mint::init_siz = -1;
int mint::mintP = P;
vector<mint> mint::factorial, mint::inverse;
long long n, p;
long long pow(long long n, long long k) {
  long long out = 1;
  for (int i = 0; i < k; i++) {
    if (out > n)
      return n + 1;
    else
      out *= n;
  }
  return out;
}
void solve() {
  cin >> n >> p;
  vector<long long> k(n);
  for (int i = 0; i < n; i++) cin >> k[i];
  if (p == 1) {
    if (n & 1)
      cout << "1\n";
    else
      cout << "0\n";
    return;
  }
  sort(k.begin(), k.end());
  vector<pair<int, int> > proc;
  for (int i = 0; i < n; i++) {
    if (proc.empty() || proc.back().first != k[i])
      proc.push_back({k[i], 1});
    else
      proc.back().second++;
  }
  int N = (int)((proc).size()), i = N - 1;
  for (; i >= 0; i--) {
    if (proc[i].second & 1) {
      long long tot = -1;
      int ind = i - 1;
      for (; ind >= 0; ind--) {
        tot = pow(p, (proc[ind + 1].first - proc[ind].first)) * tot +
              proc[ind].second;
        if (tot >= 0) break;
        if (tot < -n) break;
      }
      if (tot < 0) break;
      for (int j = i - 1; j > ind; j--) proc[j].second = 0;
      if (ind >= 0) proc[ind].second = tot;
    }
    proc[i].second = 0;
  }
  mint out = 0;
  for (int j = 0; j < i; j++)
    out += proc[j].second * mint::exp(p, proc[j].first);
  if (i >= 0) out = mint::exp(p, proc[i].first) - out;
  cout << out << '\n';
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  cin >> T;
  for (int c = 1; c < T + 1; c++) {
    solve();
  }
}
