#include <bits/stdc++.h>
using namespace std;
template <class T>
string tostring(T x) {
  ostringstream out;
  out << x;
  return out.str();
}
long long toint(string s) {
  istringstream in(s);
  long long x;
  in >> x;
  return x;
}
int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int dy[8] = {1, -1, 0, 0, -1, 1, -1, 1};
int kx[8] = {1, 1, -1, -1, 2, 2, -2, -2};
int ky[8] = {2, -2, 2, -2, 1, -1, 1, -1};
const int MAX = 1111111;
const int MOD = 1000000007;
int n, p;
int a[MAX];
map<int, int> m;
long long big = 1111111111111LL;
long long fastpow(long long k) {
  long long mult = p, res = 1;
  while (k > 0) {
    if (k % 2) {
      res = res * mult % MOD;
    }
    k /= 2;
    mult = mult * mult % MOD;
  }
  return res;
}
void solve() {
  long long ans = 0;
  cin >> n >> p;
  m.clear();
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    m[x]++;
  }
  if (p == 1) {
    cout << n % 2 << endl;
    return;
  }
  vector<pair<int, int> > v;
  for (auto it = m.begin(); it != m.end(); it++) {
    v.push_back(make_pair(it->first, it->second));
  }
  for (int i = v.size() - 1; i >= 0;) {
    int x = v[i].second;
    if (x == 0) {
      i--;
      continue;
    }
    if (x % 2) {
      if (i == 0) {
        cout << fastpow(v[i].first) << endl;
        return;
      }
      long long diff = 1;
      while (i > 0) {
        i--;
        long long ndiff = diff;
        bool stop = false;
        for (int j = 0; j < v[i + 1].first - v[i].first; j++) {
          ndiff *= p;
          if (ndiff > big) {
            stop = true;
            break;
          }
        }
        if (v[i].second >= ndiff) {
          v[i].second -= ndiff;
          break;
        } else {
          if (stop || i == 0) {
            long long ans1 = diff % MOD * fastpow(v[i + 1].first) % MOD;
            long long ans2 = 0;
            for (int k = i; k >= 0; k--) {
              ans2 += fastpow(v[k].first) * v[k].second % MOD;
              ans2 %= MOD;
            }
            cout << ((ans1 - ans2) % MOD + MOD) % MOD << endl;
            return;
          }
          diff = ndiff - v[i].second;
        }
      }
    } else {
      i--;
    }
  }
  cout << 0 << endl;
}
int main() {
  std::ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
