#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e3 + 23;
const int maxn = 2e5 + 35;
const int mod = 1e9 + 7;
long long qpow(long long a, long long first) {
  long long res = 1;
  while (first) {
    if (first & 1) res = res * a % mod;
    a = a * a % mod;
    first >>= 1;
  }
  return res;
}
int main() {
  int T, n, p, k;
  scanf("%d", &T);
  while (T--) {
    scanf("%d %d", &n, &p);
    map<int, int> mp, vis;
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &k);
      mp[k] += 1;
    }
    if (p == 1) {
      if (n & 1)
        puts("1");
      else
        puts("0");
      continue;
    }
    for (map<int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {
      if (it->second >= p) {
        vis[it->first + 1] = 1;
        mp[it->first + 1] += it->second / p;
        it->second = it->second % p;
      }
    }
    long long ans = 0;
    bool flag = false;
    for (map<int, int>::reverse_iterator it = mp.rbegin(); it != mp.rend();
         ++it) {
      if (flag) {
        ans = (ans - qpow(p, it->first) * it->second % mod + mod) % mod;
        continue;
      }
      if (it->second & 1) {
        if (vis.count(it->first))
          mp[it->first - 1] += p;
        else {
          ans = qpow(p, it->first);
          flag = true;
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
