#include <bits/stdc++.h>
using namespace std;
long long A[1100000], mod = 1e9 + 7;
bool com(int a, int b) { return a > b; }
long long qp(long long a, long long b) {
  long long ans = 1, k = 1;
  while (k <= b) {
    if (k & b) ans = (ans * a) % mod;
    k *= 2;
    a = (a * a) % mod;
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n;
    long long q;
    scanf("%d%lld", &n, &q);
    for (int i = 1; i <= n; i++) scanf("%lld", &A[i]);
    sort(A + 1, A + 1 + n, com);
    long long num = 0, ans = 0, k = 0;
    bool flag = 0;
    if (q == 1)
      ans = (n % 2);
    else
      for (int i = 1; i <= n; i++) {
        if (!num) {
          num++;
          k = A[i];
          ans = qp(q, A[i]);
        } else {
          while (k > A[i]) {
            k--;
            num *= q;
            if (n - i + 1 <= num) {
              for (int j = i + 1; j <= n; j++)
                ans = (ans + mod - qp(q, A[j])) % mod;
              flag = 1;
              break;
            }
          }
          num--;
          k = A[i];
          ans = (ans + mod - qp(q, A[i])) % mod;
        }
        if (flag) break;
      }
    printf("%lld\n", ans % mod);
  }
}
