#include <bits/stdc++.h>
#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
long long mod = 1e9 + 7;
long long binpow(long long x, long long k) {
  x %= mod;
  if (k < 2) return k ? x : 1;
  return (binpow(x * x, k / 2) * (k & 1 ? x : 1)) % mod;
}
const long long inf = 2e6;
long long a, p;
map<long long, long long> mp;
deque<pair<long long, long long> > n;
long long s1, s2;
void solve() {
  if (!n.size()) return;
  if (n[0].second % 2 == 0) {
    n.pop_front();
    solve();
    return;
  }
  n[0].second %= 2;
  long long need = p, cur = n[0].first - 1, fl = 0, ps = -1;
  for (int q = 1; q < n.size(); q++) {
    for (; cur > n[q].first && need < inf;) {
      cur--;
      need *= p;
    }
    if (need >= inf) break;
    if (n[q].second < need)
      need -= n[q].second;
    else {
      fl = 1;
      n[q].second -= need;
      need = 0;
      ps = q;
      break;
    }
  }
  if (fl == 1) {
    for (int q = 0; q < ps; q++) n.pop_front();
    solve();
    return;
  } else {
    s1 = binpow(p, n[0].first);
    for (int q = 1; q < n.size(); q++) {
      s2 += binpow(p, n[q].first) * n[q].second;
      s2 %= mod;
    }
  }
}
int main() {
  cin.tie(0);
  cout.tie(0);
  cin.sync_with_stdio(0);
  cout.sync_with_stdio(0);
  ;
  long long z;
  cin >> z;
  for (; z--;) {
    cin >> a >> p;
    mp.clear();
    n.clear();
    s1 = s2 = 0;
    for (int q = 0; q < a; q++) {
      long long x;
      cin >> x;
      mp[x]++;
    }
    if (p == 1) {
      cout << a % 2 << "\n";
      continue;
    }
    for (pair<long long, long long> t : mp) n.push_back(t);
    reverse(n.begin(), n.end());
    solve();
    long long O = s1 - s2;
    if (O < 0) O += mod;
    cout << O << "\n";
  }
}
