#include <bits/stdc++.h>
using namespace std;
long long int mod_power(long long int n, long long int k) {
  long long int ans = 1;
  while (k != 0) {
    if (k % 2 != 0) {
      ans = ans * n;
      ans = ans % 1000000007;
    }
    n = n * n;
    n = n % 1000000007;
    k = k / 2;
  }
  return ans;
}
void call(vector<int> &v, int p, int x) {
  int n = v.size();
  while (n >= p && n >= 1 && v[n - 1] == v[n - p] && p > 1) {
    int k = v[n - 1], j = p;
    while (j--) {
      v.pop_back();
    }
    v.push_back(k + 1);
    n = v.size();
  }
}
int main() {
  long long int t = 1;
  cin >> t;
  while (t--) {
    long long int n, p;
    cin >> n >> p;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    long long int l = 0, m = 0;
    vector<int> v1, v2;
    sort(a, a + n, greater<int>());
    v1.push_back(a[0]);
    if (n > 1) {
      v2.push_back(a[1]);
      for (int i = 2; i < n; i++) {
        if (v1.size() > v2.size() || v1.back() > v2.back()) {
          v2.push_back(a[i]);
          call(v2, p, a[i]);
        } else {
          v1.push_back(a[i]);
          call(v1, p, a[i]);
        }
      }
    }
    long long int ans1 = 0, ans2 = 0;
    for (int i = 0; i < v1.size(); i++) {
      long long int temp = mod_power(p, v1[i]);
      ans1 = ans1 + temp;
      ans1 %= 1000000007;
    }
    for (int i = 0; i < v2.size(); i++) {
      long long int temp = mod_power(p, v2[i]);
      ans2 = ans2 + temp;
      ans2 %= 1000000007;
    }
    long long int ans = ans1 - ans2;
    if (p == 1) {
      if (n % 2 == 0)
        ans = 0;
      else
        ans = 1;
    }
    if (ans < 0) ans = ans + 1000000007;
    cout << ans << "\n";
  }
  return 0;
}
