#include <bits/stdc++.h>
using namespace std;
int x[1000005];
long long modexp(long long a, long long n) {
  long long r = 1, c = a;
  while (n) {
    if (n & 1) r = (r * c) % 1000000007;
    c = (c * c) % 1000000007;
    n >>= 1;
  }
  return r;
}
long long power(long long a, long long n) {
  long long r = 1;
  while (n--) {
    if (r * a < 1000005)
      r *= a;
    else
      return 1000005;
  }
  return r;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    for (int i = 0; i < n; i++) cin >> x[i];
    if (p == 1)
      cout << n % 2 << endl;
    else {
      long long ans = 0;
      int i = n - 1;
      sort(x, x + n);
      while (i >= 0) {
        long long req = 1;
        int j = i - 1;
        while (j >= 0 && req > 0) {
          req *= power(p, x[j + 1] - x[j]);
          if (req > 1000005) req = 1000005;
          j--;
          req--;
        }
        ans = (ans + modexp(p, x[i])) % 1000000007;
        j++;
        int tmp = j;
        while (j < i) {
          ans = (ans - modexp(p, x[j])) % 1000000007;
          j++;
        }
        i = tmp - 1;
      }
      cout << (ans + 1000000007) % 1000000007 << endl;
    }
  }
  return 0;
}
