#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
long long big = 1000000000000;
long long modulo(long long a) { return (a % mod + mod) % mod; }
long long binpow(long long a, long long n) {
  if (n == 0ll) return 1ll;
  long long res = binpow(a, n / 2ll);
  res = modulo(res * res);
  if (n % 2ll == 1ll) return res = modulo(res * a);
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int tc = 0; tc < t; tc++) {
    int n;
    cin >> n;
    long long p;
    cin >> p;
    vector<long long> k;
    for (int i = 0; i < n; i++) {
      long long kin;
      cin >> kin;
      k.push_back(kin);
    }
    sort(k.begin(), k.end());
    reverse(k.begin(), k.end());
    if (p == 1ll) {
      if (n % 2 == 0)
        cout << 0 << "\n";
      else
        cout << 1 << "\n";
      continue;
    }
    int ind = 0;
    while (ind < n) {
      long long val = 1ll;
      int j = ind + 1;
      bool toobig = false;
      while (j < n) {
        for (int i = 0; i < k[j - 1] - k[j]; i++) {
          val *= p;
          if (val > big) {
            break;
            toobig = true;
          }
        }
        if (toobig) break;
        val--;
        j++;
        if (val == 0ll) {
          ind = j;
          break;
        }
      }
      if (val != 0ll) break;
    }
    if (ind >= n) {
      cout << "0"
           << "\n";
      continue;
    }
    long long res = binpow(p, k[ind]);
    for (int i = ind + 1; i < n; i++) {
      res = modulo(res - binpow(p, k[i]));
    }
    cout << res << "\n";
  }
}
