#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int ans = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-') f = -1;
  for (; isdigit(c); c = getchar()) ans = (ans << 3) + (ans << 1) + (c ^ 48);
  return ans * f;
}
inline int cmin(int &a, int b) { return a = min(a, b); }
inline int cmax(int &a, int b) { return a = max(a, b); }
const int mod = 1000000007;
int k[1000005];
inline long long qpow(long long a, int k) {
  long long ans = 1;
  while (k) {
    if (k & 1) ans = ans * a % mod;
    a = a * a % mod;
    k >>= 1;
  }
  return ans;
}
int main() {
  int T = read();
  while (T--) {
    int n = read();
    long long p = read();
    for (int i = 1; i <= n; i++) k[i] = read();
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    sort(k + 1, k + n + 1, greater<int>());
    long long a = 0, b = 0, t = 0, flag = 0;
    for (int i = 1; i <= n; i++) {
      if (i > 1 && t) {
        for (int j = k[i - 1] - 1; j >= k[i]; j--) {
          t *= p;
          if (t >= n - i + 1) {
            for (; i <= n; i++) (b += qpow(p, k[i])) %= mod;
            flag = 1;
            break;
          }
        }
      }
      if (flag) break;
      if (t)
        (b += qpow(p, k[i])) %= mod, t--;
      else
        (a += qpow(p, k[i])) %= mod, t = 1;
    }
    cout << (a - b + mod) % mod << endl;
  }
  return 0;
}
