#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
long long pwr(long long a, long long pw) {
  long long ret = 1;
  while (pw > 0) {
    if (pw & 1) {
      ret = ret * a % mod;
    }
    a = a * a % mod;
    pw >>= 1;
  }
  return ret;
}
int n, p;
vector<int> v;
void solve_case() {
  cin >> n >> p;
  v.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (p == 1) {
    cout << n % 2 << "\n";
    return;
  }
  sort(v.begin(), v.end());
  map<int, int> mp1, mp2;
  for (int i = n - 1; i >= 0; i--) {
    int a = v[i];
    if ((int)mp2.size() > 0) {
      while (true) {
        mp1[a]++;
        if (mp1[a] == p) {
          mp1.erase(mp1.find(a));
          a++;
        } else {
          break;
        }
      }
    } else {
      mp2[a]++;
    }
    while ((int)mp1.size() && (int)mp2.size() &&
           prev(mp1.end())->first == prev(mp2.end())->first) {
      int val = prev(mp1.end())->first;
      int sub = min(mp1[val], mp2[val]);
      mp1[val] -= sub;
      mp2[val] -= sub;
      if (mp1[val] == 0) {
        mp1.erase(mp1.find(val));
      }
      if (mp2[val] == 0) {
        mp2.erase(mp2.find(val));
      }
    }
  }
  long long ans = 0;
  for (auto aa : mp2) {
    ans = (ans + 1ll * aa.second * pwr(p, aa.first) % mod) % mod;
  }
  for (auto aa : mp1) {
    ans = (ans - 1ll * aa.second * pwr(p, aa.first) % mod) % mod;
  }
  if (ans < 0) {
    ans += mod;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve_case();
  }
  return 0;
}
