#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const double Dlt = 0.97;
const int inf = 0x3f3f3f3f;
const long long INF = 2e18;
const int maxn = 1000000 + 10;
const int maxm = 500 + 10;
const long long mod = 1000000000 + 7;
const long long Mod = 1000000000 + 3;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
inline long long read() {
  char ch = getchar();
  long long x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int n, p;
int a[maxn];
long long cal(long long a, long long b, long long MOD) {
  long long res = 1;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
  }
  return res % MOD;
}
int main() {
  int T = read();
  while (T--) {
    n = read(), p = read();
    for (int i = 1; i <= n; ++i) a[i] = read();
    sort(a + 1, a + 1 + n);
    reverse(a + 1, a + 1 + n);
    long long ans = 0, res = 0;
    for (int i = 1; i <= n; ++i) {
      if (!res && !ans) {
        ans = (ans + cal(p, a[i], mod)) % mod;
        res = (res + cal(p, a[i], Mod)) % Mod;
      } else {
        ans = (ans - cal(p, a[i], mod) + mod) % mod;
        res = (res - cal(p, a[i], Mod) + Mod) % Mod;
      }
    }
    printf("%lld\n", ans);
  }
}
