#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
inline long long sub(long long &a, long long b) {
  a -= b;
  if (a < 0) {
    a += MOD;
  }
}
inline long long mult(long long a, long long b) { return (a * 1ll * b) % MOD; }
inline long long b_pow(long long a, long long p) {
  long long cur = a, ans = 1;
  while (p > 0) {
    if (p & 1) {
      ans = mult(ans, cur);
    }
    p /= 2;
    cur = mult(cur, cur);
  }
  return ans;
}
const long long lim = 1e6;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long T;
  cin >> T;
  while (T--) {
    long long n, p;
    cin >> n >> p;
    if (p == 1) {
      for (long long i = 0; i < n; i++) {
        long long x;
        cin >> x;
      }
      cout << (n & 1) << '\n';
      continue;
    }
    map<long long, long long> cnt;
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      cnt[x]++;
    }
    auto it = --cnt.end();
    long long ans = 0, need = 0;
    for (;;) {
      if (it->second >= need) {
        it->second -= need;
        need = 0;
        ans = 0;
      } else {
        sub(ans, mult(it->second, b_pow(p, it->first)));
        need -= it->second;
        it->second = 0;
      }
      if (it->second & 1) {
        need = 1;
        ans = b_pow(p, it->first);
      }
      if (it == cnt.begin()) {
        break;
      } else {
        long long was = it->first;
        it--;
        if (need > 0) {
          while (need < 1e6 && was != it->first) {
            need *= p;
            was--;
          }
        }
      }
    }
    cout << ans << '\n';
  }
}
