#include <bits/stdc++.h>
using namespace std;
long long int n, k, m;
long long int i, p;
long long int expo(long long int a, long long int b) {
  long long int x = b;
  long long int ans = 1, y = a;
  while (x > 0) {
    if (x % 2 == 1) {
      ans = (ans * y) % 1000000007;
    }
    y = (y * y) % 1000000007;
    x = x / 2;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  vector<long long int> arr(1000005);
  int j, i;
  int g = t;
  while (t--) {
    long long int p1 = 0, p2 = 0;
    cin >> n >> p;
    for (i = 0; i < n; i++) {
      cin >> arr[i];
    }
    if (p == 1) {
      cout << n % 2 << "\n";
      continue;
    }
    sort(arr.begin(), arr.begin() + n, greater<int>());
    p1 = arr[0];
    long long int curr_sum = expo(p, arr[0]);
    map<long long int, long long int> m;
    for (i = 1; i < n; i++) {
      if (m[p1] == 1) {
        curr_sum = (curr_sum + expo(p, arr[i])) % 1000000007;
        p1 = arr[i];
        m[p1] = 0;
      } else {
        curr_sum = (curr_sum - expo(p, arr[i]) + 1000000007) % 1000000007;
        if (m.find(arr[i]) == m.end()) {
          m[arr[i]] = 1;
        } else {
          m[arr[i]]++;
          long long int curr = arr[i];
          while (m[curr] == p) {
            m[curr] = 0;
            if (m.find(curr + 1) == m.end()) {
              m[curr + 1] = 1;
            } else {
              m[curr + 1]++;
            }
            curr++;
          }
        }
      }
    }
    cout << curr_sum << "\n";
  }
  return 0;
}
