#include <bits/stdc++.h>
using namespace std;
inline int read() {
  char ch = getchar();
  int f = 1, x = 0;
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f * x;
}
long long MOD = 1e9 + 7;
long long mod = 999998639;
long long ksm(long long a, long long n, long long mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * a) % mod;
    n >>= 1;
    a = a * a % mod;
  }
  return res;
}
int t;
int n;
int p;
const int maxv = 1e6 + 10;
int k[maxv];
int main() {
  t = read();
  while (t--) {
    n = read();
    p = read();
    for (int i = 1; i <= n; i++) k[i] = read();
    sort(k + 1, k + 1 + n, greater<int>());
    long long ans1 = 0;
    long long ans2 = 0;
    for (int i = 1; i <= n; i++) {
      if (ans1 == 0 && ans2 == 0) {
        ans1 = (ans1 + ksm(p, k[i], MOD)) % MOD;
        ans2 = (ans2 + ksm(p, k[i], mod)) % mod;
      } else {
        ans1 = (ans1 - ksm(p, k[i], MOD) + MOD) % MOD;
        ans2 = (ans2 - ksm(p, k[i], mod) + mod) % mod;
      }
    }
    printf("%lld\n", ans1);
  }
  return 0;
}
