#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
long long pow_mod(long long p, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n % 2) res *= p;
    p *= p;
    n /= 2;
    p %= mod;
    res %= mod;
  }
  return res % mod;
}
long long pow_p(long long p, long long n) {
  long long res = 1;
  while (n > 0) {
    if (n % 2) res *= p;
    p *= p;
    n /= 2;
  }
  return res;
}
signed main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> k(n);
    for (long long i = 0; i < n; ++i) cin >> k[i];
    sort(k.begin(), k.end());
    reverse(k.begin(), k.end());
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    } else {
      long long dp = 1;
      long long dc = 0;
      vector<long long> v1, v2;
      bool yes = false;
      long long lk = 0;
      for (long long i = 0; i < n; ++i) {
        if (!yes) {
          v1.push_back(k[i]);
          dp = 1;
          dc = 0;
          lk = k[i];
          yes = true;
        } else {
          v2.push_back(k[i]);
          if (dp > n) continue;
          while (lk - k[i] > dc) {
            ++dc;
            dp *= p;
            if (dp > n) break;
          }
          if (dp > n)
            continue;
          else {
            dp -= pow_p(p, dc - (lk - k[i]));
            if (dp == 0) yes = false;
          }
        }
      }
      long long ans = 0;
      for (long long i = 0; i < v1.size(); ++i) {
        ans += pow_mod(p, v1[i]);
        ans %= mod;
      }
      for (long long i = 0; i < v2.size(); ++i) {
        ans -= pow_mod(p, v2[i]);
        ans %= mod;
      }
      cout << (ans % mod + mod) % mod << '\n';
    }
  }
  cin >> t;
  return 0;
}
