#include <bits/stdc++.h>
using namespace std;
int T, N;
int P;
vector<int> v, A, B;
long long pw(long long a, int n) {
  long long ret = 1;
  while (n) {
    if (n & 1) ret = ret * a % 1000000007;
    a = a * a % 1000000007;
    n >>= 1;
  }
  return ret;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    v.clear();
    A.clear();
    B.clear();
    scanf("%d %d", &N, &P);
    v.resize(N);
    for (int i = 0; i < N; i++) scanf("%d", &v[i]);
    if (P == 1) {
      if (v.size() % 2)
        printf("1\n");
      else
        printf("0\n");
      continue;
    }
    sort(v.rbegin(), v.rend());
    int i = 0;
    while (i < N) {
      A.push_back(v[i]);
      long long need = 1;
      int prv = v[i];
      i++;
      while (i < N) {
        B.push_back(v[i]);
        int now = v[i];
        i++;
        if (prv - now >= 20) break;
        while (prv > now) {
          need *= P;
          prv--;
          if (need > N) break;
        }
        if (need > N) break;
        need--;
        if (need == 0) break;
      }
      if (need) {
        while (i < N) {
          B.push_back(v[i]);
          i++;
        }
      }
    }
    long long res = 0;
    for (int k : A) {
      res += pw(P, k);
      res %= 1000000007;
    }
    for (int k : B) {
      res -= pw(P, k);
      res %= 1000000007;
    }
    res += 1000000007;
    res %= 1000000007;
    printf("%lld\n", res);
  }
  return 0;
}
