#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
long long power(long long x, long long p) {
  long long res = 1;
  while (p) {
    if (p & 1) {
      res = (res * x) % mod;
    }
    x = (x * x) % mod;
    p >>= 1;
  }
  return res;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) {
      cin >> v[i];
    }
    if (p == 1) {
      cout << (n & 1) << "\n";
      continue;
    }
    sort(v.rbegin(), v.rend());
    long long res = power(p, v[0]);
    long long prev = v[0];
    long long val = 1;
    for (long long i = 1; i < n; i++) {
      if (val == 0) {
        val = 1;
        prev = v[i];
        res = (res + power(p, v[i]));
      } else {
        res = (res - power(p, v[i]) + mod) % mod;
        if (val < n) {
          while (val < n && prev != v[i]) {
            prev--;
            val *= p;
          }
          if (val < n) {
            val--;
          }
        }
      }
    }
    cout << res << "\n";
  }
}
