#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
const long long inf = 1e18;
const double PI = acos(-1);
const double eps = 1e-8;
long long qkmod(long long base, long long cnt) {
  long long res = 1;
  while (cnt) {
    if (cnt & 1) res = res * base % mod;
    base = base * base % mod;
    cnt >>= 1;
  }
  return res;
}
int a[maxn];
int main() {
  int T;
  scanf("%d", &T);
  int n, p;
  while (T--) {
    scanf("%d %d", &n, &p);
    for (int i = 0; i < n; i++) cin >> a[i];
    if (p == 1) {
      printf(n & 1 ? "1\n" : "0\n");
      continue;
    }
    sort(a, a + n);
    long long res = 0, sum = 0, pre = 1e6 + 7;
    bool flag = false;
    int i = n - 1, j = n - 1;
    while (~i) {
      int k = a[i--];
      int d = pre - k;
      pre = k;
      res = res * qkmod(p, d) % mod;
      for (int i = 0; i < min(20, d); i++) {
        sum *= p;
        if (sum > maxn) flag = true;
      }
      while (~j && a[j] == k) {
        j--;
        if (flag || sum > 0) {
          sum--;
          res += mod - 1;
        } else {
          sum++;
          res++;
        }
      }
      res %= mod;
    }
    printf("%d\n", (res * qkmod(p, pre)) % mod);
  }
}
