#include <bits/stdc++.h>
using namespace std;
int n;
long long k[1000005], p;
int mod = 1e9 + 7;
long long cal(long long cur) {
  if (cur == 0) return 1;
  if (cur == 1) return p;
  if (cur % 2 == 1) return cal(cur / 2) % mod * cal(cur / 2) % mod * p % mod;
  return (cal(cur / 2) % mod) * cal(cur / 2) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) cin >> k[i];
    if (p == 1) {
      if (n % 2 == 0)
        cout << 0 << endl;
      else
        cout << 1 << endl;
    } else {
      sort(k + 1, k + 1 + n);
      long long s = 0;
      long long v = 0;
      long long cur = k[n];
      bool flag = 1;
      for (int i = n; i >= 1; i--) {
        while (cur > k[i] && flag) {
          v = v * p;
          cur--;
          if (v > i) flag = 0;
          if (v == 0) cur = k[i];
        }
        if (flag) {
          if (v == 0) {
            s = (s + cal(k[i])) % mod;
            v++;
            if (v > i - 1) flag = 0;
          } else {
            s = (s - cal(k[i]) + 2 * mod) % mod;
            v--;
          }
        } else {
          s = (s - cal(k[i]) + 2 * mod) % mod;
        }
      }
      cout << s << endl;
    }
  }
}
