#include <bits/stdc++.h>
const long long mod = 1e9 + 7;
long long k[1000005];
long long count[1000005];
int a[1000005];
int have[1000005];
long long power(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
long long truePower(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}
int getUpper(long long p, long long upper) {
  long long base = 1, res = 0;
  while (base * p <= upper) {
    base *= p;
    res++;
  }
  return res;
}
long long min(long long a, long long b) { return a > b ? b : a; }
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    long long n, p;
    scanf("%lld%lld", &n, &p);
    int size = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &k[i]);
      if (count[k[i]] == 0) {
        count[k[i]] = 1;
        a[++size] = k[i];
      } else
        count[k[i]]++;
    }
    std::sort(a + 1, a + 1 + size);
    int gap = -1;
    if (p != 1)
      gap = getUpper(p, 1000000);
    else
      gap = 1000000;
    for (int i = 1; i <= size - 1; i++) {
      int u = a[i], v = a[i + 1];
      if (v - u > gap)
        have[i] = 0;
      else {
        long long rate = truePower(p, v - u);
        have[i] = (count[u] + have[i - 1]) / rate;
      }
    }
    int ptr = size;
    while (ptr >= 1) {
      int u = a[ptr];
      if (count[u] % 2 == 0)
        ptr--;
      else if (ptr >= 1 && have[ptr - 1] != 0) {
        int np = ptr - 1;
        count[u] = 1;
        while (np >= 1) {
          count[u] = count[u] * truePower(p, a[np + 1] - a[np]);
          long long minus = min(count[u], count[a[np]]);
          count[u] -= minus;
          count[a[np]] -= minus;
          if (count[u] == 0)
            break;
          else
            np--;
        }
        ptr = np;
      } else
        break;
    }
    if (ptr == 0)
      printf("0\n");
    else {
      long long ans = power(p, a[ptr]);
      for (int i = ptr - 1; i >= 1; i--) {
        int u = a[i];
        ans -= power(p, u) * count[u] % mod;
        if (ans < 0) ans += mod;
      }
      printf("%lld\n", ans);
    }
    for (int i = 1; i <= size; i++) count[a[i]] = 0;
  }
  return 0;
}
