#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int MOD = 1e9 + 7;
int v[N], fr[N];
int calc(int x, int p, int y) {
  if (p == 1) {
    return x;
  }
  int ans = 1;
  for (int i = 1; i <= y; i++) {
    if (1LL * ans * p > N) {
      return -1;
    }
    ans *= p;
  }
  if (1LL * x * ans > N) {
    return -1;
  }
  return x * ans;
}
int lgpow(int a, int b) {
  int rez = 1;
  while (b) {
    if (b & 1) rez = (1LL * rez * a) % MOD;
    a = (1LL * a * a) % MOD;
    b /= 2;
  }
  return rez;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    int n, p;
    cin >> n >> p;
    for (int i = 1; i <= n; i++) {
      cin >> v[i];
      fr[v[i]]++;
    }
    sort(v + 1, v + n + 1);
    reverse(v + 1, v + n + 1);
    int new_n = 1;
    for (int i = 2; i <= n; i++)
      if (v[i] != v[i - 1]) v[++new_n] = v[i];
    n = new_n;
    int pos = 1, ans = 0;
    while (pos <= n) {
      if (fr[v[pos]] % 2 == 0) {
        pos++;
        continue;
      }
      int need = 1, x = v[pos];
      bool ok = 0;
      for (int i = pos + 1; i <= n; i++) {
        int new_need = calc(need, p, x - v[i]);
        if (new_need == -1) break;
        if (new_need <= fr[v[i]]) {
          fr[v[i]] -= new_need;
          pos = i;
          ok = 1;
          break;
        } else {
          need = new_need - fr[v[i]];
          x = v[i];
        }
      }
      if (!ok) {
        ans = lgpow(p, v[pos]);
        for (int i = pos + 1; i <= n; i++)
          ans = (ans - (1LL * fr[v[i]] * lgpow(p, v[i])) % MOD + MOD) % MOD;
        break;
      }
    }
    cout << ans << "\n";
    for (int i = 1; i <= n; i++) fr[v[i]] = 0;
  }
}
