#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<db, db>;
using vi = vector<int>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<string>;
using vii = vector<pii>;
using vll = vector<pll>;
using vdd = vector<pdd>;
template <class T>
bool ckmin(T& a, const T& b) {
  return (b < a) ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return (a < b) ? a = b, 1 : 0;
}
const ll MOD = 1e9 + 7;
const ll MOD2 = 999995713;
const int kx[] = {0, 1, 0, -1}, ky[] = {1, 0, -1, 0};
void fileIO(string filename) {
  freopen((filename + ".in").c_str(), "r", stdin);
  freopen((filename + ".out").c_str(), "w", stdout);
}
ll binpow(ll a, ll b, ll mod) {
  ll ans = 1;
  while (b > 0) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    ll p, n;
    cin >> n >> p;
    vl a(n);
    for (int(i) = 0; i < n; ++i) cin >> a[i];
    sort((a).begin(), (a).end(), greater<ll>());
    ll ans = 0, buf = 0;
    for (int(i) = 0; i < n; ++i) {
      if (!ans && !buf) {
        ans += binpow(p, a[i], MOD);
        buf += binpow(p, a[i], MOD2);
      } else {
        ans = (ans - binpow(p, a[i], MOD) + MOD) % MOD;
        buf = (buf - binpow(p, a[i], MOD2) + MOD2) % MOD2;
      }
    }
    cout << ans << '\n';
  }
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
}
