#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 5, MOD = 1e9 + 7;
long long t, n, p, k[N];
long long power(long long b, long long e) {
  long long p = 1, q = b;
  for (; e; e >>= 1) {
    if (e & 1) p = (p * q) % MOD;
    q = (q * q) % MOD;
  }
  return p;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (long long i = 0; i < n; i++) cin >> k[i];
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    }
    sort(k, k + n);
    long long ans = 0;
    for (long long i = n - 1; i >= 0; i--) {
      long long ex = k[i], el = 1, o = i;
      for (long long j = i - 1; j >= 0; j--) {
        if (k[j] < ex) {
          for (; ex > k[j] && el <= j + 1; ex--, el *= p)
            ;
          if (k[j] < ex) break;
        }
        el--;
        if (!el) {
          i = j;
          break;
        }
      }
      if (i == o) {
        ans = power(p, k[i]);
        for (long long l = 0; l < i; l++)
          ans = (ans - power(p, k[l]) + MOD) % MOD;
        break;
      }
    }
    cout << ans << '\n';
  }
}
