#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  bool b = 0;
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  if (c == '-') c = getchar(), b = 1;
  x = c - 48;
  while (isdigit(c = getchar())) x = (x << 3) + (x << 1) + c - 48;
  if (b) x = -x;
}
template <typename T>
inline void wrip(T x) {
  if (x > 9) wrip(x / 10);
  putchar(x % 10 + 48);
}
template <typename T>
inline void write(T x) {
  if (x < 0) putchar('-'), x = -x;
  wrip(x);
  putchar(' ');
}
template <typename T>
inline void writeln(T x) {
  if (x < 0) putchar('-'), x = -x;
  wrip(x);
  putchar('\n');
}
inline char readch() {
  char c;
  do c = getchar();
  while (c == '\n' || c == ' ');
  return c;
}
inline void reads(string &s) {
  char c;
  while ((c = getchar()) == ' ' || c == '\n')
    ;
  s = c;
  while ((c = getchar()) != ' ' && c != '\n' && c != EOF) s += c;
}
inline void getlines(string &s) {
  char c;
  while ((c = getchar()) == '\n')
    ;
  s = c;
  while ((c = getchar()) != '\n') s += c;
}
const long double pi = acos(-1);
const int inf = 1e9, mod = 1e9 + 7, N = 1e6 + 11;
int n;
long long m, a[N], cnt[N], pwm[N];
inline long long pw(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b % 2) res = res * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return res;
}
int main() {
  int ntest;
  read(ntest);
  while (ntest--) {
    read(n);
    read(m);
    for (int i = 1; i <= n; i++) read(a[i]);
    if (m == 1) {
      if (n % 2)
        cout << 1 << '\n';
      else
        cout << 0 << '\n';
      continue;
    }
    for (int i = 1; i <= n; i++) cnt[a[i]]++;
    sort(a + 1, a + n + 1);
    n = unique(a + 1, a + n + 1) - a - 1;
    reverse(a + 1, a + n + 1);
    long long lim = 2e6, res = 0;
    long long Max = 0;
    pwm[0] = 1;
    for (int i = 1; i <= 1e6; i++) {
      pwm[i] = pwm[i - 1] * m;
      if (pwm[i] >= 1e7) {
        Max = i;
        break;
      }
    }
    for (int i = 1; i <= n; i++) {
      if (cnt[a[i]] % 2 == 0) continue;
      long long num = 1, sum = 0;
      for (int j = i + 1; j <= n; j++) {
        num *= pwm[min(Max, a[j - 1] - a[j])];
        if (num <= cnt[a[j]]) {
          cnt[a[j]] -= num;
          goto line0;
        }
        sum = (sum + pw(m, a[j]) * cnt[a[j]]) % mod;
        num -= cnt[a[j]];
        cnt[a[j]] = 0;
        if (num >= lim) break;
      }
      for (int j = i + 1; j <= n; j++)
        if (cnt[a[j]]) sum = (sum + pw(m, a[j]) * cnt[a[j]]) % mod;
      res = (pw(m, a[i]) - sum + mod) % mod;
      break;
    line0:;
    }
    for (int i = 1; i <= n; i++) cnt[a[i]] = 0;
    cout << res << '\n';
  }
}
