#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
  if (y == 0) return 1;
  long long int z = power(x, y / 2);
  z = z * z;
  z %= 1000000007;
  if (y & 1) return (z * x) % 1000000007;
  return z;
}
int main() {
  ios_base ::sync_with_stdio(false);
  ;
  cin.tie(NULL);
  ;
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, p, i, j, k;
    cin >> n >> p;
    map<long long int, long long int> mp;
    for (i = 0; i < n; i++) {
      cin >> k;
      mp[k]++;
    }
    if (p == 1) {
      if (n & 1)
        cout << 1;
      else
        cout << 0;
      cout << "\n";
      continue;
    }
    long long int ans = 0;
    map<long long int, long long int> val;
    long long int strt = -1;
    long long int largest = -1;
    for (auto it = mp.rbegin(); it != mp.rend(); it++) {
      long long int cnt = it->second;
      long long int num = it->first;
      while (cnt && strt != -1) {
        val[num]++;
        cnt--;
        ans = (ans - (power(p, num)) + 1000000007) % 1000000007;
        long long int x = num;
        while (val[x] == p) {
          val.erase(x);
          x++;
          val[x]++;
          largest = max(largest, x);
        }
        if (largest == strt) {
          largest = strt = -1;
          val.clear();
        }
      }
      if (strt == -1) {
        cnt %= 2;
        if (cnt & 1) {
          ans += power(p, num);
          ans %= 1000000007;
          strt = num;
        }
      }
    }
    cout << ans << "\n";
  }
}
