#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long n, p, k[1000006], num[1000006];
vector<int> a, b;
long long pw(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long ans = 1;
  long long gt = pw(x, y / 2);
  ans *= gt;
  ans %= mod;
  ans *= gt;
  ans %= mod;
  if (y % 2) {
    ans *= x;
    ans %= mod;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (int i = 0; i < n; i++) {
      cin >> k[i];
      if (num[k[i]] == 0) a.push_back(k[i]);
      num[k[i]]++;
    }
    if (p == 1) {
      a.clear();
      for (int i = 0; i < n; i++) num[k[i]] = 0;
      cout << n % 2 << endl;
      continue;
    }
    sort(a.begin(), a.end());
    long long nd = 0, ans = 0;
    for (int i = a.size() - 1; i >= 0; i--) {
      if (nd == 0) {
        if (num[a[i]] % 2) {
          nd = 1;
          ans += pw(p, a[i]) % mod + mod;
          ans %= mod;
        }
      } else {
        ans -= pw(p, a[i]) * min(nd, num[a[i]]) % mod;
        ans += mod;
        ans %= mod;
        if (num[a[i]] >= nd) {
          num[a[i]] -= nd;
          nd = 0;
        } else {
          nd -= num[a[i]];
          num[a[i]] = 0;
        }
        if (num[a[i]] % 2) {
          nd = 1;
          ans += pw(p, a[i]) % mod + mod;
          ans %= mod;
        }
      }
      if (i != 0 && nd != 0) {
        int x = a[i] - a[i - 1];
        while (x--) {
          nd *= p;
          if (nd > 1e6) {
            nd = 1e9;
            break;
          }
        }
      }
    }
    for (int i = 0; i < n; i++) num[k[i]] = 0;
    cout << ans << endl;
    a.clear();
  }
}
