#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10, mod = 1e9 + 7;
int n, p;
int fp(int a, int b) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % mod;
    b >>= 1;
    a = 1ll * a * a % mod;
  }
  return ret;
}
int main() {
  int tc = 1;
  scanf("%d", &tc);
  for (int cn = 1; cn <= tc; cn++) {
    scanf("%d%d", &n, &p);
    map<int, int, greater<int>> cnt;
    for (int i = 0; i < n; i++) {
      int x;
      scanf("%d", &x);
      cnt[x]++;
    }
    if (p == 1) {
      printf("%d\n", n % 2);
      continue;
    }
    int ans = 0;
    bool bo = 0;
    int dif = 0;
    int lst = -1;
    for (auto it : cnt) {
      if (lst < 0) {
        dif = it.second % 2;
        if (dif) ans = fp(p, it.first);
        lst = it.first;
        continue;
      }
      if (bo)
        ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
      else {
        int delta = lst - it.first;
        if (!dif) delta = 0;
        for (; 1ll * dif * p <= 1e6 && delta > 0; dif *= p, delta--)
          ;
        if (delta) {
          bo = 1;
          ans = (ans - 1ll * it.second * fp(p, it.first) % mod) % mod;
        } else {
          int can = -min(dif, it.second);
          if (abs(can) % 2 != it.second % 2) can++;
          int x = (1ll * can + it.second) / 2;
          ans = (ans + 1ll * x * fp(p, it.first) % mod) % mod;
          ans = (ans - 1ll * (it.second - x) * fp(p, it.first) % mod) % mod;
          dif += can;
        }
      }
      lst = it.first;
    }
    if (ans < 0) ans += mod;
    printf("%d\n", ans);
  }
  return 0;
}
