#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y);
bool isPrime(long long int n);
long long int modInv(long long int a, long long int b);
long long int gcdExtended(long long int a, long long int b, long long int *x,
                          long long int *y);
long long int mpower(long long int a, long long int b, long long int p);
const long long int inf = (long long int)1e17;
const long long int N = (long long int)3e5 + 5;
long long int pw(long long int x, long long int n,
                 long long int mod = 1000000007) {
  long long int ans = 1;
  x %= mod;
  while (n > 0) {
    if (n & 1) {
      ans = (ans * x) % 1000000007;
      ans %= 1000000007;
    }
    x = (x * x) % 1000000007;
    x %= 1000000007;
    n = n / 2;
  }
  return ans;
}
void solve() {
  long long int n, p;
  cin >> n >> p;
  vector<long long int> v(n);
  for (long long int i = 0; i < n; ++i) cin >> v[i];
  if (p == 1) {
    cout << n % 2 << endl;
    return;
  }
  sort(v.begin(), v.end());
  reverse(v.begin(), v.end());
  vector<pair<long long int, long long int> > tmp;
  long long int cur = -1;
  for (long long int i = 0; i < n; ++i) {
    if (cur == -1) {
      cur = v[i];
      continue;
    }
    long long int x = v[i];
    while (!tmp.empty() && tmp.back() == make_pair(x, p - 1))
      tmp.pop_back(), ++x;
    if (tmp.empty() || tmp.back().first != x)
      tmp.push_back({x, 1});
    else {
      auto p = tmp.back();
      tmp.pop_back();
      ++p.second;
      tmp.push_back(p);
    }
    if (cur == tmp.back().first) {
      tmp.pop_back();
      cur = -1;
    }
  }
  if (cur == -1) {
    cout << 0 << endl;
    return;
  }
  long long int ans = pw(p, cur);
  for (auto c : tmp)
    ans = (ans - ((c.second * pw(p, c.first)) % 1000000007) + 1000000007) %
          1000000007;
  cout << ans << endl;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
long long int modInv(long long int a, long long int m) {
  long long int x, y;
  long long int g = gcdExtended(a, m, &x, &y);
  long long int res = (x % m + m) % m;
  return res;
}
long long int gcdExtended(long long int a, long long int b, long long int *x,
                          long long int *y) {
  if (a == 0) {
    *x = 0, *y = 1;
    return b;
  }
  long long int x1, y1;
  long long int gcd = gcdExtended(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long int mpower(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) res = res * x;
    y = y >> 1;
    x = x * x;
  }
  return res;
}
bool isPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  long long int p = sqrt(n);
  for (long long int i = 5; i <= p; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
