#include <bits/stdc++.h>
using namespace std;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1};
int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2};
int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1};
long long bigmod(long long a, long long b, long long c) {
  if (b == 0) return 1 % c;
  long long x = bigmod(a, b / 2, c);
  x = (x * x) % c;
  if (b % 2 == 1) x = (x * a) % c;
  return x;
}
long long mod_inverse(long long a, long long mod) {
  return bigmod(a, mod - 2, mod);
}
long long MX = 3e6 + 1;
long long poww(long long a, long long b) {
  if (b == 0) return 1;
  long long x = poww(a, b / 2);
  x = min(MX, x * x);
  if (b % 2 == 1) x = (x * a);
  return min(MX, x);
}
long long frq[100005 * 10];
vector<pair<long long, long long> > aa, bb;
long long a[100005 * 10];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t, tt = 1;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    aa.clear();
    bb.clear();
    long long mx = 0;
    set<int> st;
    for (int i = 0; i < (n); i++) {
      cin >> a[i];
      frq[a[i]]++;
      mx = max(a[i], mx);
      st.insert(a[i]);
    }
    if (p == 1) {
      if (n % 2 == 0)
        cout << 0 << endl;
      else
        cout << 1 << endl;
      for (auto x : st) frq[x] = 0;
      continue;
    }
    vector<long long> v;
    for (auto x : st) v.push_back(x);
    int sz = v.size();
    for (int ii = sz - 1; ii >= 0; ii--) {
      long long i = v[ii];
      if (!frq[i]) continue;
      int mid = frq[i] / 2;
      if (mid) {
        aa.push_back({i, mid});
        bb.push_back({i, mid});
      }
      if (frq[i] % 2) {
        long long need = 1;
        long long tot = 0;
        long long pre = v[ii];
        bool flg = 0;
        aa.push_back({i, 1});
        for (long long jj = ii - 1; jj >= 0; jj--) {
          long long j = v[jj];
          long long df = pre - j;
          long long now = need * poww(p, df);
          pre = j;
          if (frq[j] >= now) {
            flg = 1;
            break;
          } else {
            need = now - frq[j];
          }
          if (need > n) break;
        }
        if (!flg) {
          for (int jj = 0; jj < ii; jj++) {
            int j = v[jj];
            if (frq[j]) bb.push_back({j, frq[j]});
          }
          break;
        }
        bb.push_back({i, 1});
        need = 1;
        tot = 0;
        pre = v[ii];
        for (long long jj = ii - 1; jj >= 0; jj--) {
          long long j = v[jj];
          long long df = pre - j;
          long long now = need * poww(p, df);
          pre = j;
          if (frq[j] >= now) {
            frq[j] -= now;
            break;
          } else {
            need = now - frq[j];
            frq[j] = 0;
          }
        }
      }
    }
    long long sum = 0;
    for (auto x : aa) {
      long long md = bigmod(p, x.first, 1000000007);
      md *= x.second;
      md %= 1000000007;
      sum += md;
      sum %= 1000000007;
    }
    for (auto x : bb) {
      long long md = bigmod(p, x.first, 1000000007);
      md *= x.second;
      md %= 1000000007;
      sum -= md;
      sum %= 1000000007;
      if (sum < 0) sum += 1000000007;
    }
    cout << sum << endl;
    for (auto x : st) frq[x] = 0;
  }
  return 0;
}
