#include <bits/stdc++.h>
using namespace std;
vector<pair<long long int, long long int> > vec;
vector<long long int> arr;
long long int M;
bool vis[1000009];
long long int bigmod(long long int a, long long int b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 0) {
    long long int x = bigmod(a, b / 2);
    return x = (x * x) % M;
  } else {
    long long int x = bigmod(a, b - 1);
    return x = (a * x) % M;
  }
}
int main() {
  long long int a, b, c, d, e, i, j, k, l, n, m, x, y, t, pp, p;
  scanf("%lld", &t);
  M = 1e9 + 7;
  for (pp = 1; pp <= t; pp++) {
    scanf("%lld%lld", &n, &p);
    arr.clear();
    vec.clear();
    for (i = 1; i <= n; i++) {
      scanf("%lld", &a);
      arr.push_back(a);
      vis[i] = false;
    }
    if (p == 1) {
      a = n % 2;
      printf("%lld\n", a);
      continue;
    }
    sort(arr.begin(), arr.end());
    for (i = 0; i < n; i++) {
      a = arr[i];
      b = 0;
      while (a == arr[i] && i < n) {
        b++;
        i++;
      }
      i--;
      vec.push_back({a, b});
    }
    l = vec.size();
    for (i = 0; i < (l - 1); i++) {
      a = vec[i + 1].first - vec[i].first;
      b = vec[i].second;
      d = b;
      c = 0;
      while (b >= p) {
        b = b / p;
        c++;
      }
      if (c >= a) {
        vis[i + 1] = true;
        e = powl(p, a);
        vec[i].second = (d % e);
        vec[i + 1].second += (d / e);
      }
    }
    x = y = 0;
    for (i = l - 1; i >= 0; i--) {
      a = vec[i].first;
      b = vec[i].second;
      if (b % 2 == 1) {
        if (vis[i] == true) {
          c = vec[i].first - vec[i - 1].first;
          c = powl(p, c);
          vec[i - 1].second += c;
        } else {
          c = bigmod(p, a);
          x = c;
          for (j = i - 1; j >= 0; j--) {
            c = bigmod(p, vec[j].first);
            c = (c * vec[j].second) % M;
            y = (y + c) % M;
          }
          break;
        }
      }
    }
    x = (x - y) % M;
    x = (x + M) % M;
    printf("%lld\n", x);
  }
}
