#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
long long pp(long long x, long long y) {
  long long ret = 1;
  long long curr = x;
  for (int i = 0; i < 40; i++) {
    if (y & (1LL << i)) ret = (ret * curr) % 1000000007;
    curr = (curr * curr) % 1000000007;
  }
  return ret;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> a(n);
    long long maxx = 0, k = 1;
    if (p == 1)
      maxx = 10000000000;
    else {
      while (true) {
        k *= p;
        if (k > 1e6) break;
        maxx++;
      }
    }
    for (long long i = 0; i < n; i++) cin >> a[i];
    map<long long, long long> ct;
    for (auto it : a) ct[it]++;
    long long sum = 0;
    vector<pair<long long, long long>> x;
    for (auto it = ct.rbegin(); it != ct.rend(); it++) {
      if (x.empty()) {
        if (it->second % 2 == 0)
          continue;
        else
          x.push_back({it->first, 1}),
              sum = (sum + pp(p, it->first)) % 1000000007;
      } else {
        while (!x.empty()) {
          if (it->second == 0) break;
          long long xx = x.back().first;
          long long need = (xx - it->first);
          if (p == 1) need = 1;
          if (need > maxx) {
            sum = (sum + 1000000007 -
                   ((pp(p, it->first) * it->second) % 1000000007)) %
                  1000000007;
            it->second = 0;
            break;
          } else {
            long long m = pp(p, need) * x.back().second;
            if (m > it->second) {
              sum = (sum + 1000000007 -
                     ((pp(p, it->first) * it->second) % 1000000007)) %
                    1000000007;
              m -= it->second;
              it->second = 0;
              if (m < 1e6) {
                x.pop_back();
                x.push_back({it->first, m});
              }
              break;
            } else {
              sum = (sum + 1000000007 - ((pp(p, it->first) * m) % 1000000007)) %
                    1000000007;
              x.pop_back();
              it->second -= m;
              break;
            }
          }
        }
        if (it->second > 0) {
          if (it->second % 2 == 1)
            sum = (sum + pp(p, it->first)) % 1000000007,
            x.push_back({it->first, 1});
        }
      }
    }
    cout << sum << "\n";
  }
  return 0;
}
