#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 6, MOD = 1e9 + 7;
int n, p;
int arr[N];
int co[N], mod[N];
vector<pair<int, int> > v;
set<int> ss;
long long poww(long long x, long long y) {
  if (y == 0) return 1;
  if (y & 1) return (poww(x, y - 1) * x) % MOD;
  return poww((x * x) % MOD, y / 2);
}
long long sum(int x) {
  long long ans = 0;
  for (int i = v.size() - 1; i > x; i--) {
    ans += v[i].second * poww(p, v[i].first);
    ans %= MOD;
  }
  return ans;
}
int main() {
  int tc;
  scanf("%d", &tc);
  while (tc--) {
    ss.clear();
    v.clear();
    scanf("%d %d", &n, &p);
    for (int i = 0; i < n; i++) {
      scanf("%d", &arr[i]);
      co[arr[i]]++;
      ss.insert(arr[i]);
    }
    if (p == 1) {
      for (auto it : ss) {
        co[it] = 0;
        mod[it] = 0;
      }
      printf("%d\n", n % 2);
      continue;
    }
    int f = (*ss.begin());
    while (true) {
      if (co[f] >= p) {
        co[f + 1] += co[f] / p;
        mod[f + 1] = 1;
        co[f] %= p;
        ss.insert(f + 1);
      }
      auto it = ss.upper_bound(f);
      if (it == ss.end()) break;
      f = *it;
    }
    for (auto it : ss) v.push_back({it, co[it]});
    reverse(v.begin(), v.end());
    long long ans = 0;
    for (int i = 0; i < v.size(); i++) {
      if ((v[i].second & 1) && (mod[v[i].first] == 0)) {
        ans = (poww(p, v[i].first) - sum(i) + MOD) % MOD;
        break;
      }
      if ((v[i].second & 1) && (p & 1)) {
        assert(v[i + 1].first == v[i].first - 1);
        v[i + 1].second += p;
      }
    }
    for (auto it : ss) {
      co[it] = 0;
      mod[it] = 0;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
