#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int t, n, p, k[maxn];
const int P = 1e9 + 7;
bool Pow(int &val, int p, int siz, int mul) {
  long long sum = 1, z = val;
  bool ok = 0;
  for (; p; p >>= 1) {
    if (p & 1) sum = sum * z;
    if (p) z = z * z;
    if (sum > P || (p > 1 && z > P)) ok = 1;
    sum %= P;
    z %= P;
  }
  if ((long long)sum * mul > siz) ok = 1;
  val = (long long)sum * mul % P;
  return ok;
}
int Pow(int a, int b) {
  long long sum = 1, z = a;
  for (; b; b >>= 1) {
    if (b & 1) sum = sum * z % P;
    if (b) z = z * z % P;
  }
  return sum;
}
int solve(int now) {
  int to;
  for (to = now; k[to] == k[now]; ++to)
    ;
  if (to > n) return (to - now) % 2;
  int val = solve(to), siz = to - now;
  if (!val) return siz & 1;
  int tmp = p;
  if (Pow(tmp, k[to] - k[now], siz, val))
    return (tmp - siz + P) % P;
  else
    return (siz & 1) ^ (tmp & 1);
}
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d", &n, &p);
    for (int i = 1; i <= n; i++) scanf("%d", &k[i]);
    k[n + 1] = -1;
    sort(k + 1, k + n + 1);
    printf("%lld\n", (long long)solve(1) * Pow(p, k[1]) % P);
  }
}
