#include <bits/stdc++.h>
using namespace std;
const long long int N = 200001;
const long long int M = 21;
struct point {
  long long int x, y, cur;
  bool operator==(const point b) {
    if (this->x == b.x && this->y == b.y) return 1;
    return 0;
  }
  bool operator!=(const point b) { return !(*this == b); }
  friend bool operator<(const point a, const point b) {
    if (a.x != b.x) return a.x < b.x;
    return a.y < b.y;
  }
};
void debug(stringstream &s) {}
template <typename T, typename... Args>
void debug(stringstream &s, T a, Args... args) {
  string word;
  s >> word;
  cout << word << " = " << a << endl;
  debug(s, args...);
}
bool overflow;
long long int fast_exp(long long int x, long long int n) {
  if (n == 0) return 1;
  long long int ans;
  if (n % 2 == 0) {
    ans = fast_exp(x, n / 2);
    if (ans > ans * ans % 1000000007) overflow = 1;
    ans = ans * ans % 1000000007;
  } else {
    ans = fast_exp(x, n - 1);
    if (ans > ans * x % 1000000007) overflow = 1;
    ans = ans * x % 1000000007;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int _ = 1;
  cin >> _;
  while (_--) {
    long long int n, m = 0, x = 0, y = 1, ans = 0, p;
    cin >> n >> p;
    long long int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    if (n == 1)
      ans = fast_exp(p, a[0]);
    else if (p == 1) {
      ans = n % 2;
    } else {
      sort(a, a + n);
      long long int sum = 1, over = 0;
      ans = fast_exp(p, a[n - 1]);
      for (int i = n - 2; i >= 0; i--) {
        if (over) {
          ans -= fast_exp(p, a[i]);
          ans = (ans + 1000000007) % 1000000007;
          continue;
        }
        if (sum == 0) {
          sum = sum * fast_exp(p, a[i + 1] - a[i]) + 1;
          ans += fast_exp(p, a[i]);
        } else {
          overflow = 0;
          sum = sum * fast_exp(p, a[i + 1] - a[i]) - 1;
          if (overflow) over = 1;
          ans -= fast_exp(p, a[i]);
        }
        if (sum >= n) {
          over = 1;
        }
        ans = (ans + 1000000007) % 1000000007;
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
