#include <bits/stdc++.h>
using namespace std;
const long long P1 = 1e9 + 7, P2 = 1e9 + 3;
long long t, n, p, ans1, ans2, k[1000005];
long long ksm(long long x, long long y, long long P) {
  long long res = 1;
  while (y) {
    if (y & 1) res = res * x % P;
    x = x * x % P;
    y >>= 1;
  }
  return res;
}
int main() {
  for (scanf("%lld", &t); t--;) {
    scanf("%lld%lld", &n, &p);
    for (long long i = 1; i <= n; i++) scanf("%lld", k + i);
    sort(k + 1, k + 1 + n);
    ans1 = ans2 = 0;
    for (long long i = n; i >= 1; i--) {
      if (!ans1 && !ans2)
        ans1 = (ans1 + ksm(p, k[i], P1)) % P1,
        ans2 = (ans2 + ksm(p, k[i], P2)) % P2;
      else
        ans1 = (ans1 - ksm(p, k[i], P1) + P1) % P1,
        ans2 = (ans2 - ksm(p, k[i], P2) + P2) % P2;
    }
    printf("%lld\n", ans1);
  }
}
