#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000 * 1000 * 1000 + 7;
const long long MOD2 = 999994537;
const long long N = 1000 * 1000 + 5;
const double PI = 3.14159265;
long long search(vector<long long> arr, long long l, long long r, long long x) {
  if (r >= l) {
    long long mid = l + (r - l) / 2;
    if (arr[mid] == x) return mid;
    if (arr[mid] > x) return search(arr, l, mid - 1, x);
    return search(arr, mid + 1, r, x);
  }
  return -1;
}
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long power(long long x, long long y, long long p) {
  long long res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long modInverse(long long n, long long p) { return power(n, p - 2, p); }
const int maxn = 3e5 + 5;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long n, p, ans = 0, a = 0;
    scanf("%lld%lld", &n, &p);
    vector<long long> k(n);
    for (int i = 0; i < n; i++) scanf("%lld", &k[i]);
    sort(k.rbegin(), k.rend());
    for (int i = 0; i < n; i++) {
      if (!ans && !a) {
        ans += power(p, k[i], MOD);
        a += power(p, k[i], MOD2);
      } else {
        ans = (ans - power(p, k[i], MOD) + MOD) % MOD;
        a = (a - power(p, k[i], MOD2) + MOD2) % MOD2;
      }
    }
    printf("%lld\n", ans);
  }
}
