#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int MAXN = (int)1e5 + 5;
const int mod1 = (int)1e9 + 7;
const int mod2 = 9191891;
const int INF = 0x3f3f3f3f;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
long long powmod(long long a, long long b, long long mod) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, p;
    cin >> n >> p;
    vector<int> k(n);
    for (int i = 0; i < n; i++) cin >> k[i];
    sort(k.begin(), k.end(), greater<int>());
    long long ans1, ans2;
    ans1 = ans2 = 0;
    for (int i = 0; i < n; i++) {
      long long tmp1 = powmod(p, k[i], mod1), tmp2 = powmod(p, k[i], mod2);
      if (ans1 == 0 && ans2 == 0)
        ans1 = tmp1, ans2 = tmp2;
      else
        ans1 = (ans1 - tmp1 + mod1) % mod1, ans2 = (ans2 - tmp2 + mod2) % mod2;
    }
    cout << ans1 << endl;
  }
  return 0;
}
