#include <bits/stdc++.h>
using namespace std;
mt19937_64 mrand(chrono::steady_clock::now().time_since_epoch().count());
inline int read() {
  char c = getchar();
  int x = 0, f = 1;
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
  return x * f;
}
const int mod = 1e9 + 7;
long long fpow(long long a, long long b) {
  long long r = 1;
  for (a %= mod; b; b >>= 1) {
    if (b & 1) r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
int main() {
  int t = read();
  while (t--) {
    int n = read(), p = read();
    priority_queue<pair<long long, long long> > q;
    if (p == 1) {
      for (int i = 0; i < n; ++i) read();
      printf("%d\n", n & 1);
      continue;
    }
    for (int i = 0; i < n; ++i) {
      int a = read();
      q.push({a, 1});
    }
    long long ans = 0;
    int f = 0;
    while (q.size() > 1) {
      auto p1 = q.top();
      q.pop();
      auto p2 = q.top();
      q.pop();
      long long cur1 = p1.first, cnt1 = p1.second;
      long long cur2 = p2.first, cnt2 = p2.second;
      assert(cur1 >= cur2);
      while (cur1 > cur2) {
        --cur1;
        cnt1 = cnt1 * p;
        if (cnt1 > n) {
          ans = fpow(p, cur1) * cnt1 % mod;
          ans -= fpow(p, cur2) * cnt2 % mod;
          while (!q.empty()) {
            auto tmp = q.top();
            q.pop();
            ans -= fpow(p, tmp.first) * tmp.second % mod;
          }
          f = 1;
          break;
        }
      }
      if (f) break;
      cnt1 -= cnt2;
      assert(cnt1 >= 0);
      if (cnt1) q.push({cur1, cnt1});
    }
    if (!q.empty()) ans = fpow(p, q.top().first) * q.top().second % mod;
    ans %= mod;
    if (ans < 0) ans += mod;
    printf("%lld\n", ans);
  }
}
