#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int INF = 1e9 * 2;
const long long LINF = (long long)1e18;
const double EPS = 1e-7;
const int mod = 998244353;
const int MOD = 1e9 + 7;
struct segment_Treee {
  struct node {
    long long set;
    long long min;
  };
  vector<node> t;
  long long no_operation = LLONG_MIN;
  long long op_mod(long long a, long long b) {
    if (b == no_operation) return a;
    return b;
  }
  long long op_mn(long long a, long long b) { return min(a, b); }
  void propagate(int v, int tl, int tr) {
    if (t[v].set == no_operation || tr - tl == 1) return;
    int tm = (tl + tr) / 2;
    t[v * 2 + 1].set = op_mod(t[v * 2 + 1].set, t[v].set);
    t[v * 2 + 1].min = op_mod(t[v * 2 + 1].min, t[v].set);
    t[v * 2 + 2].set = op_mod(t[v * 2 + 2].set, t[v].set);
    t[v * 2 + 2].min = op_mod(t[v * 2 + 2].min, t[v].set);
    t[v].set = no_operation;
  }
  int sz = 1;
  long long neutral = LLONG_MAX;
  void build(int n) {
    while (sz < n) sz *= 2;
    t.resize(2 * sz - 1);
  }
  void update(int v, int tl, int tr, int l, int r, int val) {
    propagate(v, tl, tr);
    if (l >= tr || r <= tl) return;
    if (l <= tl && r >= tr) {
      t[v].set = op_mod(t[v].set, val);
      t[v].min = op_mod(t[v].min, val);
      return;
    }
    int tm = (tl + tr) / 2;
    update(v * 2 + 1, tl, tm, l, r, val);
    update(v * 2 + 2, tm, tr, l, r, val);
    t[v].min = op_mn(t[v * 2 + 1].min, t[v * 2 + 2].min);
  }
  void update(int l, int r, int val) { update(0, 0, sz, l, r, val); }
  long long get(int v, int tl, int tr, int l, int r) {
    propagate(v, tl, tr);
    if (l >= tr || r <= tl) return neutral;
    if (tl >= l && tr <= r) return t[v].min;
    int tm = (tl + tr) / 2;
    long long mn1 = get(v * 2 + 1, tl, tm, l, r);
    long long mn2 = get(v * 2 + 2, tm, tr, l, r);
    return op_mn(mn1, mn2);
  }
  long long get(int l, int r) { return get(0, 0, sz, l, r); }
} T;
const int nax = 1e6 + 10;
void mul(int &a, int b) { a = a * 1ll * b % MOD; }
int cnt[nax];
void sub(int &a, int b) {
  a -= b;
  if (a < 0) a += MOD;
}
int dod(int a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
  return a;
}
int vid(int a, int b) {
  a -= b;
  if (a < 0) a += MOD;
  return a;
}
void add(int &a, int b) {
  a += b;
  if (a >= MOD) a -= MOD;
}
int binpow(int a, int n) {
  int res = 1;
  while (n) {
    if (n & 1) mul(res, a);
    mul(a, a);
    n >>= 1;
  }
  return res;
}
void sub(int &a, long long b) {
  a = 1ll * a - b;
  if (a < 0) a += MOD;
}
int func(int count, int p, int num) {
  int ans = 1;
  for (int i = (0); i < (num); i++) {
    if (ans * 1ll * p > INF) return -1;
    ans *= p;
  }
  if (ans * 1ll * count > INF) return -1;
  return count * ans;
}
int mul_1(int a, int b) { return a * 1ll * b % MOD; }
void solve() {
  int n, p;
  cin >> n >> p;
  vector<int> vec;
  vector<int> a(n);
  for (int i = (0); i < (n); i++) {
    cin >> a[i];
    if (cnt[a[i]] == 0) vec.push_back(a[i]);
    ++cnt[a[i]];
  }
  if (p == 1) {
    for (int x : vec) cnt[x] = 0;
    cout << (n % 2 == 0 ? 0 : 1) << endl;
    return;
  }
  int diff = 0, pos = 0;
  sort(vec.begin(), vec.end(), greater<int>());
  n = (int)vec.size();
  while (pos < n) {
    if (cnt[vec[pos]] % 2 == 0) {
      pos++;
      continue;
    }
    int x = vec[pos];
    int nd = 1, is_good = 0;
    for (int position = pos + 1; position < n; position++) {
      int new_nd = func(nd, p, (x - vec[position]));
      if (new_nd == -1) {
        break;
      }
      if (new_nd <= cnt[vec[position]]) {
        cnt[vec[position]] -= new_nd;
        pos = position;
        is_good = 1;
        break;
      } else {
        nd = new_nd - cnt[vec[position]];
        x = vec[position];
      }
    }
    if (!is_good) {
      diff = binpow(p, vec[pos]);
      for (int i = pos + 1; i < n; i++) {
        sub(diff, mul_1(cnt[vec[i]], binpow(p, vec[i])));
      }
      break;
    }
  }
  cout << diff << endl;
  for (int x : vec) cnt[x] = 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int test_case;
  cin >> test_case;
  while (test_case--) {
    solve();
  }
}
