#include <bits/stdc++.h>
int faster_in() {
  int r = 0, c;
  for (c = getchar(); c <= 32; c = getchar())
    ;
  if (c == '-') return -faster_in();
  for (; c > 32; r = (r << 1) + (r << 3) + c - '0', c = getchar())
    ;
  return r;
}
using namespace std;
const int INF = 1234567890;
long long mod = 1e9 + 7;
void add(long long &a, long long b) { a = (a + b) % mod; }
long long modPow(long long b, long long e) {
  long long res = 1;
  while (e > 0) {
    if (e & 1) {
      res = res * b % mod;
    }
    b = b * b % mod;
    e >>= 1;
  }
  return res;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    unordered_map<int, int> freq;
    long long a[n];
    for (int i = (int)0, o_o = n; i < (int)o_o; ++i) cin >> a[i];
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    }
    sort(a, a + n);
    long long need = -1;
    long long A = 0, B = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (need == -1) {
        add(B, modPow(p, a[i]));
        need = a[i];
      } else {
        add(A, modPow(p, a[i]));
        freq[a[i]]++;
        int pot = a[i];
        while (freq[pot] >= p) {
          freq[pot] -= p;
          freq[++pot]++;
        }
        if (pot == need) {
          need = -1;
          freq[pot]--;
        }
      }
    }
    cout << (B - A + mod) % mod << '\n';
  }
  return 0;
}
