#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v);
template <typename A, typename B>
ostream &operator<<(ostream &cout, pair<A, B> const &p) {
  return cout << "(" << p.first << ", " << p.second << ")";
}
template <typename A>
ostream &operator<<(ostream &cout, vector<A> const &v) {
  cout << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i) cout << ", ";
    cout << v[i];
  }
  return cout << "]";
}
template <typename A, typename B>
istream &operator>>(istream &cin, pair<A, B> &p) {
  cin >> p.first;
  return cin >> p.second;
}
mt19937 rng(61378913);
void usaco(string filename) {
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}
const long double pi = 3.14159265358979323846;
const long long mod = 1000000007;
namespace modop {
long long madd(long long a, long long b) { return (a + b) % mod; }
long long msub(long long a, long long b) {
  return (((a - b) % mod) + mod) % mod;
}
long long mmul(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long mpow(long long base, long long exp) {
  long long res = 1;
  while (exp) {
    if (exp % 2 == 1) {
      res = (res * base) % mod;
    }
    exp >>= 1;
    base = (base * base) % mod;
  }
  return res;
}
long long minv(long long base) { return mpow(base, mod - 2); }
long long mdiv(long long a, long long b) { return mmul(a, minv(b)); }
}  // namespace modop
using namespace modop;
long long n, m, k, q, l, r, x, y, z;
long long a[1000005];
long long b[1000005];
long long c[1000005];
string second, t;
long long ans = 0;
map<long long, long long> cnt;
pair<long long, long long> vals[1000005];
void solve(int tc) {
  cin >> n >> k;
  cnt.clear();
  for (int ele = 0; ele < n; ele++) cin >> a[ele];
  ;
  if (k == 1) {
    cout << (n & 1) << '\n';
    return;
  }
  for (long long i = 0; i < (n); ++i) ++cnt[a[i]];
  long long second = cnt.size();
  int pos = 0;
  for (pair<long long, long long> x : cnt) vals[pos++] = x;
  reverse(vals, vals + second);
  long long req = 0, last = vals[0].first, ans = 0;
  bool fin = 0;
  for (long long i = 0; i < (second); ++i) {
    long long diff = last - vals[i].first;
    last = vals[i].first;
    for (long long j = 0; j < (diff); ++j) {
      if (req == 0) break;
      if (req > 1000000) {
        fin = 1;
        break;
      }
      req *= k;
      if (req > 1000000) {
        fin = 1;
        break;
      }
    }
    long long c = vals[i].second;
    if (fin) {
      ans = msub(ans, c * mpow(k, vals[i].first));
      continue;
    }
    if (c <= req) {
      req -= c;
      ans = msub(ans, c * mpow(k, vals[i].first));
    } else {
      c -= req;
      ans = 0;
      req = 0;
      if (c & 1) {
        req = 1;
        ans = (ans + mpow(k, vals[i].first)) % mod;
      }
    }
  }
  cout << (ans % mod) << '\n';
}
int main() {
  { ios_base::sync_with_stdio(false); }
  {
    cin.tie(NULL);
    cout.tie(NULL);
  }
  int tc = 1;
  cin >> tc;
  for (int t = 0; t < tc; t++) solve(t);
}
