#include <bits/stdc++.h>
using namespace std;
inline char gc() {
  static char buf[100000], *p1 = buf, *p2 = buf;
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++;
}
inline long long read() {
  long long x = 0;
  char ch = getchar();
  bool positive = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') positive = 0;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
  return positive ? x : -x;
}
inline void write(long long x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) write(x / 10);
  putchar('0' + x % 10);
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
inline void writep(long long x) {
  write(x);
  putchar(' ');
}
inline unsigned long long rnd() {
  return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = 1e6 + 5;
const long long mo1 = 1e9 + 7, mo2 = 1621836843;
int ksm(int x, int p, int mo) {
  int res = 1;
  for (; p; p >>= 1, x = (long long)x * x % mo) {
    if (p & 1) res = (long long)res * x % mo;
  }
  return res;
}
int a[N];
vector<long long> v;
int main() {
  int T = read();
  while (T--) {
    int n = read(), p = read();
    for (int i = (int)(1); i <= (int)(n); i++) a[i] = read();
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    sort(a + 1, a + n + 1);
    long long sum1 = 0, sum2 = 0;
    for (int i = (int)(n); i >= (int)(1); i--) {
      if (!sum1 && !sum2) {
        sum1 = ksm(p, a[i], mo1);
        sum2 = ksm(p, a[i], mo2);
      } else {
        sum1 = (sum1 - ksm(p, a[i], mo1) + mo1) % mo1;
        sum2 = (sum2 - ksm(p, a[i], mo2) + mo2) % mo2;
      }
    }
    cout << sum1 << endl;
  }
}
