#include <bits/stdc++.h>
using namespace std;
const long long inf = (1LL << 62);
const int iinf = (1 << 30);
const int N = 1000100;
const int MOD = 1000000007;
long long n, p, a[N], dp[N];
inline long long T(long long q) {
  if (q < 0)
    q += MOD;
  else if (q >= MOD)
    q -= MOD;
  return q;
}
long long lgput(long long x, long long y) {
  long long ans = 1;
  while (y) {
    if (y % 2) ans = (ans * x) % MOD;
    x = (x * x) % MOD;
    y /= 2;
  }
  return ans;
}
int check(int idx, long long val, long long cnt, long long &lft) {
  if (cnt > idx) {
    lft = (lft + dp[idx]) % MOD;
    return -1;
  }
  while (idx > 0 && cnt > 0 && a[idx] == val) {
    cnt--;
    lft = (lft + lgput(p, a[idx])) % MOD;
    idx--;
  }
  if (!cnt) return idx;
  if (!idx) return -1;
  if (a[idx + 1] - a[idx] >= 20) {
    lft = (lft + dp[idx]) % MOD;
    return -1;
  }
  long long aux = 1;
  for (int i = 1; i <= a[idx + 1] - a[idx]; i++, aux *= p) {
    if (aux * p > idx) {
      lft = (lft + dp[idx]) % MOD;
      return -1;
    }
  }
  return check(idx, a[idx], cnt * aux, lft);
}
void solve() {
  cin >> n >> p;
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (p == 1) {
    cout << n % 2 << '\n';
    return;
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++) dp[i] = (dp[i - 1] + lgput(p, a[i])) % MOD;
  int idx = n;
  long long on_r = 0, on_l = 0;
  while (idx > 1) {
    if (a[idx] == a[idx - 1]) {
      on_l = on_r = 0;
      idx -= 2;
      continue;
    }
    on_r = lgput(p, a[idx]);
    on_l = 0;
    if (a[idx] - a[idx - 1] >= 20) {
      on_l = dp[idx - 1];
      idx = -1;
      break;
    } else {
      long long aux = 1;
      for (int i = 1; i <= a[idx] - a[idx - 1]; i++, aux *= p) {
        if (aux * p > idx - 1) {
          on_l = dp[idx - 1];
          idx = -1;
          break;
        }
      }
    }
    if (idx < 2) break;
    idx = check(idx - 1, a[idx - 1], lgput(p, a[idx] - a[idx - 1]), on_l);
  }
  if (idx == 1)
    cout << lgput(p, a[1]) << '\n';
  else if (idx == -1)
    cout << T(on_r - on_l) << '\n';
  else
    cout << 0 << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
