#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 6;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
int T;
int n, p;
int k[maxn];
long long qpow(long long a, long long b) {
  a %= mod;
  long long ret = 1;
  while (b) {
    if (b & 1) (ret *= a) %= mod;
    (a *= a) %= mod;
    b >>= 1;
  }
  return ret;
}
priority_queue<int> q;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d", &n, &p);
    while (q.size()) q.pop();
    for (int i = 1; i <= n; i++) {
      scanf("%d", k + i);
      q.push(k[i]);
    }
    if (p == 1) {
      if (n & 1)
        puts("1");
      else
        puts("0");
      continue;
    }
    long long sum = 0, v = 0;
    int f = 0;
    int pre = 0;
    while (q.size()) {
      int top = q.top();
      q.pop();
      if (f) {
        (sum += mod - qpow(p, top)) %= mod;
      } else if (v > 0) {
        (sum += mod - qpow(p, top)) %= mod;
        for (int i = 1; i <= min(20, pre - top); i++) {
          v = v * p;
          if (v > inf) break;
        }
        v = v - 1;
        if (v > n) f = 1;
      } else if (v == 0) {
        (sum += qpow(p, top)) %= mod;
        for (int i = 1; i <= min(20, pre - top); i++) {
          v = v * p;
          if (v > inf) break;
        }
        v = v + 1;
        if (v > n) f = 1;
      }
      pre = top;
    }
    printf("%lld\n", sum);
  }
  return 0;
}
