#include <bits/stdc++.h>
using namespace std;
long long int pow_rem(long long int x, long long int y) {
  long long int res = 1;
  x = x % 1000000007;
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % 1000000007;
    }
    y = y >> 1;
    x = (x * x) % 1000000007;
  }
  return res;
}
long long int pow_def(long long int a, long long int b) {
  if (a == 1) {
    return a;
  }
  long long int res = 1;
  for (int i = 0; i < b; i++) {
    res *= a;
    if (res > 10000000) {
      break;
    }
  }
  return res;
}
void solve() {
  long long int n, p;
  cin >> n >> p;
  vector<long long int> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr.begin(), arr.end());
  long long int rem = 0;
  long long int var = 0;
  int i = n - 1;
  while (i >= 0) {
    rem += pow_rem(p, arr[i]);
    rem = rem % 1000000007;
    i--;
    if (i < 0) {
      break;
    }
    long long int p1 = pow_def(p, (arr[i + 1] - arr[i]));
    while (i >= 0 && p1) {
      rem -= pow_rem(p, arr[i]);
      rem = rem % 1000000007;
      i--;
      p1--;
      if (arr[i] != arr[i + 1]) {
        p1 = p1 * pow_def(p, arr[i + 1] - arr[i]);
      }
    }
  }
  rem = rem % 1000000007;
  if (rem < 0) {
    rem += 1000000007;
  }
  cout << rem << endl;
  return;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
