#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10;
const long long mod = 1e9 + 7;
const long long mod2 = 1e9 + 3;
long long qpow(long long a, long long b, long long p) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % p;
    a = a * a % p;
    b >>= 1;
  }
  return ans;
}
long long a[M];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    long long ans = 0, res = 0;
    for (int i = n; i >= 1; i--) {
      if (!ans & !res)
        ans = qpow(k, a[i], mod), res = qpow(k, a[i], mod2);
      else {
        ans = (ans - qpow(k, a[i], mod) + mod) % mod;
        res = (res - qpow(k, a[i], mod2) + mod2) % mod2;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
