#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long double error = 2e-6;
const long double PI = acosl(-1);
mt19937 rng((unsigned)chrono::system_clock::now().time_since_epoch().count());
inline long long int MOD(long long int x, long long int m = mod) {
  long long int y = x % m;
  return (y >= 0) ? y : y + m;
}
const int inf = 1e9;
const long long int infl = 1061109567;
const int nmax = 5e5 + 10;
long long int modexpo(long long int x, long long int n) {
  long long int ret = 1;
  while (n) {
    if (n & 1) ret = ret * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int tc;
  cin >> tc;
  for (int cs = 1; cs <= tc; cs++) {
    int n;
    long long int p;
    cin >> n >> p;
    vector<int> input(n);
    for (int i = 0; i < n; i++) cin >> input[i];
    if (p == 1) {
      cout << ((n % 2 == 0) ? 0 : 1) << "\n";
      continue;
    }
    sort(input.begin(), input.end());
    vector<pair<int, int>> freq;
    for (int i = 0; i < n; i++) {
      int j = i;
      while (j < n && input[i] == input[j]) j++;
      j--;
      freq.emplace_back(input[i], j - i + 1);
      i = j;
    }
    vector<tuple<int, int, int>> rollback;
    int cur = freq[0].first, ind = 0, carry = 0;
    while (cur != -1) {
      int f =
          (ind < freq.size() && cur == freq[ind].first) ? freq[ind].second : 0;
      f += carry;
      rollback.emplace_back(cur, carry, f % p);
      carry = f / p;
      if (ind < freq.size() && cur == freq[ind].first) ind++;
      if (carry > 0)
        cur = cur + 1;
      else if (ind < freq.size())
        cur = freq[ind].first;
      else
        cur = -1;
    }
    long long int ans = 0;
    bool bigfound = false;
    for (int i = rollback.size() - 1, carry = 0; i >= 0; i--) {
      int v, c, f;
      tie(v, c, f) = rollback[i];
      f += carry;
      if (bigfound) {
        ans -= f * modexpo(p, v);
        ans %= mod;
        carry = 0;
        continue;
      }
      if (f % 2 == 1) {
        if (c == 0) {
          ans += modexpo(p, v);
          ans %= mod;
          bigfound = true;
          carry = 0;
        } else {
          carry = p;
        }
      } else {
        carry = 0;
      }
    }
    ans = MOD(ans, mod);
    cout << ans << "\n";
  }
  return 0;
}
