#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
void pre() {}
int cnt[1000029];
const long long mod = 1000000007;
long long modpow(long long a, long long e) {
  if (e == 0) return 1;
  long long x = modpow(a * a % mod, e >> 1);
  return e & 1 ? x * a % mod : x;
}
vector<int> vx;
void solve() {
  int n, p;
  cin >> n >> p;
  vector<int> v(n);
  for (int i = 0; i < (n); ++i) cin >> v[i];
  if (p == 1) {
    cout << n % 2 << '\n';
    return;
  }
  sort(v.begin(), v.end());
  vx = v;
  long long ans = 0;
  while ((int)(v).size()) {
    int x = v.back();
    v.pop_back();
    ans = modpow(p, x);
    while ((int)(v).size() && cnt[x] == 0) {
      int y = v.back();
      ans += mod - modpow(p, y);
      while (cnt[y] == p - 1) cnt[y] = 0, y++;
      cnt[y]++;
      v.pop_back();
    }
    if ((int)(v).size()) {
      cnt[x] = 0;
    }
  }
  cout << ans % mod << '\n';
  for (auto &i : vx)
    for (int j = i; j <= (i + 20); ++j) cnt[j] = 0;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  pre();
  int n;
  cin >> n;
  for (int i = 0; i < (n); ++i) solve();
  return 0;
}
