#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6;
const long long M = 1e9 + 7;
int s[N + 5];
long long cpow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = a * ans % M;
    a = a * a % M;
    b >>= 1;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  for (int l = 1; l <= t; l++) {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> s[i];
    int p = 0;
    if (m != 1)
      while (pow(m, p) <= N) p += 1;
    else
      p = N;
    sort(s + 1, s + 1 + n, greater<int>());
    s[0] = s[1];
    long long ans = 0, cnt = 0;
    for (int i = 1; i <= n; i++) {
      long long tmp = s[i - 1] - s[i] < p ? cpow(m, s[i - 1] - s[i]) : N;
      cnt = min(N, cnt * tmp);
      if (cnt > 0) {
        cnt -= 1;
        ans = (ans + M - cpow(m, s[i])) % M;
      } else {
        cnt += 1;
        ans = (ans + cpow(m, s[i])) % M;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
