#include <bits/stdc++.h>
using namespace std;
namespace Number {
long long int gcd(long long int a, long long int b) {
  if (a % b == 0) {
    return b;
  }
  return gcd(b, a % b);
}
long long int gcd_extend(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 = gcd_extend(b % a, a, &x1, &y1);
  *x = y1 - (b / a) * x1;
  *y = x1;
  return gcd;
}
long long int mod_inv(long long int a, long long int m) {
  long long int x, y;
  long long int g = gcd_extend(a, m, &x, &y);
  long long int a_inv = (x % m + m) % m;
  return a_inv;
}
long long int mod_pow(long long int x, long long int n, long long int m) {
  if (n == 0) {
    return 1;
  } else if (n & 1) {
    long long int half = mod_pow(x, n / 2, m);
    return (half * half % m) * x % m;
  } else {
    long long int half = mod_pow(x, n / 2, m);
    return half * half % m;
  }
}
long long int mul(long long int from, long long int to, long long int m) {
  long long int ret = 1;
  for (long long int i = from; i <= to; i++) {
    ret *= i;
    ret %= m;
  }
  return ret;
}
long long int mod_comb(long long int n, long long int k, long long int m) {
  long long int mi = k < n - k ? k : n - k;
  long long int up = mul(n - mi + 1, n, m);
  long long int down = mul(1, mi, m);
  return (up * mod_inv(down, m)) % m;
}
}  // namespace Number
const int MAXN = 1e6 + 2;
int cnt[MAXN];
void clear_cnt(vector<int> &ks) {
  for (auto k : ks) cnt[k] = 0;
}
bool back_track(int idx, int P, int N) {
  long long int need = 1LL * P;
  --idx;
  while (idx >= 0 && cnt[idx] < need) {
    need = (need - cnt[idx]) * P;
    if (need > N) return false;
    --idx;
  }
  return idx >= 0;
}
void do_track(int idx, int P) {
  long long int need = 1LL * P;
  cnt[idx] = 0;
  --idx;
  while (idx >= 0 && cnt[idx] < need) {
    need = (need - cnt[idx]) * P;
    cnt[idx] = 0;
    --idx;
  }
  cnt[idx] -= need;
}
const int MOD = 1e9 + 7;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  memset(cnt, 0, sizeof(cnt));
  while (T--) {
    int N, P;
    cin >> N >> P;
    vector<int> kis;
    for (int i = 0; i < N; ++i) {
      int t;
      cin >> t;
      kis.push_back(t);
      ++cnt[t];
    }
    if (P == 1) {
      cout << N % 2 << endl;
      clear_cnt(kis);
      continue;
    }
    sort(kis.begin(), kis.end(), greater<int>());
    long long int ans = 0;
    for (int i = 0; i < kis.size(); ++i) {
      int x = kis[i];
      if (cnt[x] % 2) {
        cnt[x] = 1;
        if (back_track(x, P, N)) {
          do_track(x, P);
        } else {
          int y = kis[i];
          ans = Number::mod_pow(P, y, MOD) * cnt[y] % MOD;
          cnt[y] = 0;
          for (int j = i + 1; j < kis.size(); ++j) {
            y = kis[j];
            if (cnt[y] == 0) continue;
            ans = (ans - Number::mod_pow(P, y, MOD) * cnt[y] % MOD + MOD) % MOD;
            cnt[y] = 0;
          }
          break;
        }
      } else {
        cnt[x] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
