#include <bits/stdc++.h>
using namespace std;
int T, n, p;
int k[1000005], a[1000005], b[1000005], tot;
long long dt;
bool flag;
int ksm(int x, int y) {
  int ans = 1;
  for (; y > 0; y = y / 2) {
    if (y & 1) ans = (long long)ans * x % 1000000007;
    x = (long long)x * x % 1000000007;
  }
  return ans;
}
int main() {
  scanf("%d", &T);
  a[0] = -1;
  for (; T--;) {
    scanf("%d %d", &n, &p);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &k[i]);
    }
    if (p == 1) {
      printf("%d\n", n % 2);
      continue;
    }
    sort(k + 1, k + 1 + n);
    tot = 0;
    for (int i = n; i >= 1; i--) {
      if (k[i] == a[tot])
        b[tot]++;
      else {
        tot++;
        a[tot] = k[i];
        b[tot] = 1;
      }
    }
    dt = 0;
    flag = false;
    a[tot + 1] = 0;
    for (int i = 1; i <= tot; i++) {
      if (flag) {
        dt = (dt - (long long)b[i] * ksm(p, a[i]) % 1000000007 + 1000000007) %
             1000000007;
      } else {
        if (dt <= b[i])
          dt = (b[i] - dt) & 1;
        else
          dt -= b[i];
        if (dt == 0) continue;
        for (int j = 1; j <= a[i] - a[i + 1]; j++) {
          dt = dt * p;
          if (dt > n) {
            flag = true;
            dt = dt % 1000000007 * ksm(p, a[i] - j) % 1000000007;
            break;
          }
        }
      }
    }
    printf("%d\n", (int)(dt));
  }
  return 0;
}
