#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
long long p, n, mod = 1e9 + 7;
vector<long long> v;
pair<long long, long long> need;
long long fp(long long base, long long exp) {
  if (exp == 0) return 1;
  long long ans = fp(base, exp / 2);
  ans = (ans * ans) % mod;
  if (exp % 2 != 0) ans = (ans * (base % mod)) % mod;
  return ans;
}
bool check(long long dif) {
  long long cur = 1;
  for (int i = 1; i <= dif; i++) {
    cur *= p;
    if (cur > (int)1e6) return 1;
  }
  if (cur * need.second > (int)1e6) return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  scanf("%lld", &t);
  while (t--) {
    v.clear();
    need = {0, 0};
    scanf("%lld%lld", &n, &p);
    for (int i = 1; i <= n; i++) {
      int x;
      scanf("%d", &x);
      v.push_back(x);
    }
    sort(v.begin(), v.end());
    if (p == 1) {
      if (n % 2)
        printf("1\n");
      else
        printf("0\n");
      continue;
    }
    while (v.size()) {
      long long tmp = v.back();
      if (need.second == 0) {
        need = {tmp, 1};
        v.pop_back();
      } else {
        long long dif = need.first - tmp;
        if (check(dif)) {
          long long ans = (fp(p, need.first) * need.second) % mod;
          while (v.size()) {
            tmp = v.back();
            ans -= fp(p, tmp);
            ans = (ans + mod) % mod;
            v.pop_back();
          }
          printf("%lld\n", ans);
          goto here;
        }
        long long pw = fp(p, dif);
        need = {tmp, need.second * pw};
        need.second--;
        if (need.second == 0) need = {0, 0};
        v.pop_back();
      }
    }
    printf("%lld\n", ((fp(p, need.first)) * (need.second % mod)) % mod);
  here:
    continue;
  }
  return 0;
}
