#include <bits/stdc++.h>
using namespace std;
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long n, p;
const long long N = 1e6 + 2;
long long a[N];
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t, i, v1, ans, x, y;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    for (i = 1; i <= n; ++i) cin >> a[i];
    if (p == 1) {
      if (n % 2)
        cout << 1 << "\n";
      else
        cout << 0 << "\n";
      continue;
    }
    sort(a + 1, a + 1 + n, greater<long long>());
    stack<long long> s1, s2;
    for (i = 1; i <= n; ++i) {
      if (s1.empty()) {
        s1.push(a[i]);
        s2.push(1);
        continue;
      } else {
        if (s1.size() == 1 && a[i] == s1.top()) {
          s1.pop();
          s2.pop();
        } else if (s1.top() != a[i]) {
          s1.push(a[i]);
          s2.push(1);
        } else {
          x = s1.top();
          y = s2.top() + 1;
          if (y == p) {
            y = 1;
            ++x;
          }
          s1.pop();
          s2.pop();
          while (!s1.empty() && s1.top() == x) {
            y = y + s2.top();
            s1.pop();
            s2.pop();
            if (y == p) {
              y = 1;
              ++x;
            }
          }
          s1.push(x);
          s2.push(y);
        }
      }
      if (s1.size() == 1) {
        s1.pop();
        s2.pop();
      }
    }
    ans = 0;
    while (s1.size() > 1) {
      ans =
          (ans + ((s2.top() * binpow(p, s1.top(), 1000000007)) % 1000000007)) %
          1000000007;
      s1.pop();
      s2.pop();
    }
    if (s1.size() == 1)
      ans = ((binpow(p, s1.top(), 1000000007)) - ans) % 1000000007;
    ans = (ans + 1000000007) % 1000000007;
    cout << ans << "\n";
  }
  return 0;
}
