#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 5e5;
const long long mod = 1e9 + 7;
const int inf = 1e9 + 3;
const long long INF = 1e15;
const int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1};
const int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2};
void data() {}
int T, n, was[N];
long long p, k[N], ans;
vector<long long> v;
long long add(long long a, long long b) {
  return (a + b + mod + mod + mod) % mod;
}
long long mul(long long a, long long b) { return (a * b) % mod; }
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  long long res = 1;
  while (n) {
    if (n & 1) res = mul(res, a);
    a = mul(a, a);
    n /= 2;
  }
  return res;
}
struct nums {
  stack<pair<long long, long long> > a;
  pair<long long, long long> top() { return a.top(); }
  int size() { return (int)a.size(); }
  void init() {
    while ((int)a.size()) {
      a.pop();
    }
  }
  void make() {
    while ((int)a.size() && a.top().second == p) {
      int cnt = a.top().first + 1;
      a.pop();
      add(cnt);
    }
  }
  void add(int pw) {
    if ((int)a.size() && a.top().first == pw) {
      int cnt = a.top().second + 1;
      a.pop();
      a.push(make_pair(pw, cnt));
    } else {
      a.push(make_pair(pw, 1));
    }
    make();
  }
} a, b;
int main() {
  data();
  scanf("%d", &T);
  while (T--) {
    scanf("%d %lld", &n, &p);
    for (int i = 1; i <= n; ++i) {
      scanf("%lld", &k[i]);
    }
    ans = 0;
    if (p == 1) {
      printf("%d\n", n % 2);
      continue;
    }
    a.init();
    b.init();
    sort(k + 1, k + n + 1);
    for (int i = n; i >= 1; --i) {
      if ((a.size() == 1 && b.size() == 1 && a.top() == b.top()) ||
          (!a.size() && !b.size())) {
        a.init();
        b.init();
        a.add(k[i]);
        ans = add(ans, binpow(p, k[i]));
      } else {
        b.add(k[i]);
        ans = add(ans, -binpow(p, k[i]));
      }
    }
    printf("%lld\n", ans);
  }
}
