#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const int MOD = 1e9 + 7;
long long mul(long long a, long long b) {
  long long ans = a * b;
  if (ans >= MOD) ans = ans % MOD + MOD;
  return ans;
}
long long powMod(long long a, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = mul(ans, a);
    a = mul(a, a);
    n >>= 1;
  }
  return ans;
}
void solve() {
  int n, p;
  scanf("%d%d", &n, &p);
  map<int, int> cnt;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    cnt[x]++;
  }
  bool begin = true;
  long long need = 1;
  int before = 0;
  auto iter = (--cnt.end());
  while (1) {
    if (begin) {
      begin = false;
      need = iter->second % 2;
    } else {
      need = mul(need, powMod(p, before - iter->first));
      if (iter->second >= need) {
        need = (iter->second - need) % 2;
      } else {
        need -= iter->second;
      }
    }
    before = iter->first;
    if (iter == cnt.begin()) break;
    iter--;
  }
  long long ans = need * powMod(p, before) % MOD;
  printf("%lld\n", ans);
}
int main() {
  int T = 1;
  scanf("%d", &T);
  while (T--) solve();
  return 0;
}
