#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
const long long MOD = 1e9 + 7, mod = 999998639;
long long k[N], n, p;
long long pow_mod(long long x, long long n, long long mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (long long i = 1; i <= n; i++) cin >> k[i];
    sort(k + 1, k + 1 + n, greater<long long>());
    long long ans1 = 0, ans2 = 0;
    for (long long i = 1; i <= n; i++) {
      if (!ans1 && !ans2)
        ans1 += pow_mod(p, k[i], MOD), ans2 += pow_mod(p, k[i], mod);
      else
        ans1 = (ans1 - pow_mod(p, k[i], MOD) + MOD) % MOD,
        ans2 = (ans2 - pow_mod(p, k[i], mod) + mod) % mod;
    }
    cout << ans1 << '\n';
  }
  return 0;
}
