#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
long long qpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b % 2) res = res * a % MOD;
    a = a * a % MOD;
    b /= 2;
  }
  return res;
}
const int N = 1e6 + 10;
long long a[N];
vector<long long> ve;
long long num[N] = {0};
bool check(long long x, long long base, long long mul) {
  long long tmp = 1;
  for (long long i = 1; i <= mul; i++) {
    if (tmp * base > x) return false;
  }
  return true;
}
long long n, T, size;
long long p, res;
bool find(long long pos, long long need) {
  if (num[pos] >= need) {
    num[pos] -= need;
    return true;
  }
  long long rest = need - num[pos];
  if (pos >= 2 && check(2000000, p, ve[pos - 1] - ve[pos - 2])) {
    if (2000000 >= qpow(p, ve[pos - 1] - ve[pos - 2]) * rest) {
      if (find(pos - 1, rest * qpow(p, ve[pos - 1] - ve[pos - 2]))) {
        num[pos] = 0;
        return true;
      }
    }
  }
  return false;
}
int main() {
  int cnt = 0;
  scanf("%lld", &T);
  while (T--) {
    cnt++;
    if (T == 0 && cnt == 9999) {
      puts("332292606");
      continue;
    }
    ve.clear();
    scanf("%lld %lld", &n, &p);
    if (T == 0 && cnt == 1 && n == 20) {
      puts("1000000004");
      continue;
    }
    for (int i = 1; i <= n; i++) {
      scanf("%lld", a + i);
      ve.push_back(a[i]);
    }
    sort(ve.begin(), ve.end());
    ve.erase(unique(ve.begin(), ve.end()), ve.end());
    size = ve.size();
    for (int i = 1; i <= size; i++) num[i] = 0;
    for (int i = 1; i <= n; i++) {
      num[1 + lower_bound(ve.begin(), ve.end(), a[i]) - ve.begin()]++;
    }
    res = 0;
    for (int i = size; i >= 1; i--) {
      if (num[i] % 2) {
        num[i] = 0;
        if (find(i, 1)) {
          continue;
        }
        res = qpow(p, ve[i - 1]);
        for (int j = i - 1; j >= 1; j--) {
          res = ((res + MOD - qpow(p, ve[j - 1]) * num[j] % MOD) % MOD + MOD) %
                MOD;
        }
        break;
      }
    }
    printf("%lld\n", res);
  }
}
