#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long binpow(long long a, long long n) {
  long long res = 1;
  while (n)
    if (n % 2 == 1) {
      res = (res * a) % mod;
      n--;
    } else {
      a = (a * a) % mod;
      n = n / 2;
    }
  return res;
}
signed main() {
  std::ios_base::sync_with_stdio(false);
  long long tests;
  cin >> tests;
  for (long long test = 0; test < tests; test++) {
    long long n, p;
    cin >> n >> p;
    vector<long long> a(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    long long t = 0;
    map<long long, long long> cnt2;
    while (t < n) {
      bool eq = false;
      for (long long j = t + 1; j < n; j++) {
        cnt2[a[j]]++;
        long long x = a[j];
        while (cnt2[x] == p) {
          cnt2[x] = 0;
          cnt2[x + 1] += 1;
          x++;
        }
        if (cnt2[a[t]] == 1) {
          cnt2[a[t]] = 0;
          t = j + 1;
          eq = true;
          break;
        }
      }
      if (!eq) {
        break;
      }
    }
    if (t == n) {
      cout << 0 << endl;
      continue;
    }
    long long ans = binpow(p, a[t]);
    for (long long i = t + 1; i < n; i++) {
      ans = ans - binpow(p, a[i]);
      ans = (ans + mod) % mod;
    }
    cout << ans << endl;
  }
}
