#include <bits/stdc++.h>
using namespace std;
int t, n, p, m = 1e9 + 7, cnt[1000009];
long long fastpower(long long b, long long p) {
  if (p == 0) return 1;
  return p % 2 == 0 ? fastpower((b * b) % m, p / 2)
                    : (b * fastpower((b * b) % m, p / 2) % m) % m;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> p;
    long long ans1 = 0, ans2 = 0;
    vector<int> v;
    for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      cnt[x]++;
      if (cnt[x] == 1) v.push_back(x);
    }
    sort(v.begin(), v.end());
    if (p == 1) {
      cout << n % 2 << "\n";
      for (auto i : v) cnt[i] = 0;
      continue;
    }
    while (1 && !v.empty()) {
      if (cnt[v.back()] % 2) {
        int x = v.back();
        ans1 = fastpower(p, x);
        cnt[v.back()] = 0;
        v.pop_back();
        long long r = p, cur_k = x - 1;
        while (!v.empty() && r && r <= n) {
          if (v.back() == cur_k) {
            long long mn = min(r, 1LL * cnt[v.back()]);
            r -= mn;
            cnt[v.back()] -= mn;
            if (cnt[v.back()] == 0) v.pop_back();
            ans2 += (fastpower(p, cur_k) * mn) % m;
            ans2 %= m;
            continue;
          }
          r *= p;
          cur_k--;
        }
        if (r == 0)
          ans1 = ans2 = 0;
        else {
          while (!v.empty()) {
            ans2 += 1LL * fastpower(p, v.back()) * cnt[v.back()];
            ans2 %= m;
            cnt[v.back()] = 0;
            v.pop_back();
          }
        }
        continue;
      }
      while (!v.empty() && cnt[v.back()] % 2 == 0) {
        cnt[v.back()] = 0;
        v.pop_back();
      }
    }
    cout << (ans1 - ans2 + m) % m << "\n";
  }
}
