#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
int mod = 1e9 + 7;
int bm(int b, int p) {
  if (p == 0) return 1;
  int t = bm(b, p / 2);
  t = 1ll * t * t % mod;
  if (p & 1) return 1ll * b * t % mod;
  return t;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    int n, p;
    cin >> n >> p;
    map<int, int> M;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      M[x]++;
    }
    if (p == 1) {
      cout << (n & 1) << "\n";
      continue;
    }
    vector<pair<int, int>> pw;
    for (auto it : M) pw.push_back(it);
    int most_diff = 0;
    int mul = 1;
    vector<int> pwww = {1};
    while (true) {
      most_diff++;
      mul *= p;
      pwww.push_back(mul);
      if (mul > n) break;
    }
    int ans = 0;
    while (pw.size() > 0) {
      int k = pw.back().first, c = pw.back().second;
      pw.pop_back();
      if (c % 2 == 0) continue;
      int sz = pw.size();
      int lagbe = 1, lastk = k;
      bool can = true;
      for (int i = sz - 1; i >= 0; i--) {
        int diff = lastk - pw[i].first;
        if (diff > most_diff) {
          can = false;
          break;
        }
        long long tt = pwww[diff];
        tt *= lagbe;
        if (tt > n) {
          can = false;
          break;
        }
        lagbe = tt;
        if (lagbe <= pw[i].second) {
          lagbe = 0;
          break;
        }
        lagbe -= pw[i].second;
        lastk = pw[i].first;
      }
      if (lagbe > 0) can = false;
      if (can) {
        lagbe = 1;
        lastk = k;
        for (int i = sz - 1; i >= 0; i--) {
          int diff = lastk - pw[i].first;
          if (diff > most_diff) {
            can = false;
            break;
          }
          long long tt = pwww[diff];
          tt *= lagbe;
          if (tt > n) {
            can = false;
            break;
          }
          lagbe = tt;
          if (lagbe <= pw[i].second) {
            pw[i].second -= lagbe;
            lagbe = 0;
            if (pw[i].second == 0) pw.pop_back();
            break;
          }
          lagbe -= pw[i].second;
          lastk = pw[i].first;
          pw.pop_back();
        }
      } else {
        ans = bm(p, k);
        for (int i = 0; i < pw.size(); i++) {
          ans -= 1ll * bm(p, pw[i].first) * pw[i].second % mod;
          ans %= mod;
          ans += mod;
          ans %= mod;
        }
        ans %= mod;
        ans += mod;
        ans %= mod;
        break;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
