#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) {
  if (a == 0)
    return 0;
  else
    return (b == 0) ? a : gcd(b, a % b);
}
const long long mod1 = 1e9 + 7, mod2 = 9191891;
long long a[1000005];
long long qpow(long long a, long long b, long long p) {
  long long res = 1;
  while (b) {
    if (b & 1) {
      res = (res * a) % p;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  long long t, n, i, p;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n, greater<long long>());
    long long ans1 = 0, ans2 = 0;
    long long tp1, tp2;
    for (i = 0; i < n; i++) {
      tp1 = qpow(p, a[i], mod1), tp2 = qpow(p, a[i], mod2);
      if (ans1 == 0 && ans2 == 0) {
        ans1 = tp1, ans2 = tp2;
      } else {
        ans1 = (ans1 - tp1 + mod1) % mod1, ans2 = (ans2 - tp2 + mod2) % mod2;
      }
    }
    cout << ans1 << endl;
  }
  return 0;
}
