#include <bits/stdc++.h>
using namespace std;
const long long P = 1000000007;
const long long M = 1141919810;
long long power(long long x, long long t, long long P) {
  long long ret = 1;
  for (; t; t >>= 1, x = x * x % P) {
    if (t & 1) {
      ret = ret * x % P;
    }
  }
  return ret;
}
int32_t main() {
  ios::sync_with_stdio(false);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> k(n);
    for (long long i = 0; i < n; ++i) {
      cin >> k[i];
    }
    sort(k.begin(), k.end(), greater<long long>());
    long long ans1 = 0;
    long long ans2 = 0;
    for (long long i : k) {
      if (!ans1 && !ans2) {
        ans1 += power(p, i, P);
        ans2 += power(p, i, M);
      } else {
        ans1 = (ans1 - power(p, i, P) + P) % P;
        ans2 = (ans2 - power(p, i, M) + M) % M;
      }
    }
    cout << ans1 << endl;
  }
  return 0;
}
