#include <bits/stdc++.h>
using namespace std;
const long long mod1 = 1e9 + 7;
const long long mod2 = 0x3f3f3f3f;
const long long maxn = 1e6 + 5;
long long a[maxn];
bool cmp(long long a, long long b) { return a > b; }
long long fastpow(long long a, long long n, long long m) {
  long long base = a;
  long long res = 1;
  while (n) {
    if (n & 1) {
      res = (base * res) % m;
    }
    base = (base * base) % m;
    n >>= 1;
  }
  return res;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, p;
    scanf("%lld%lld", &n, &p);
    for (long long i = 0; i < n; i++) scanf("%lld", &a[i]);
    sort(a, a + n, cmp);
    long long cnt1 = 0, cnt2 = 0;
    for (long long i = 0; i < n; i++) {
      if (cnt1 == 0 && cnt2 == 0) {
        cnt1 = (cnt1 + fastpow(p, a[i], mod1)) % mod1;
        cnt2 = (cnt2 + fastpow(p, a[i], mod2)) % mod2;
      } else {
        cnt1 = (cnt1 - fastpow(p, a[i], mod1) + mod1) % mod1;
        cnt2 = (cnt2 - fastpow(p, a[i], mod2) + mod2) % mod2;
      }
    }
    printf("%lld\n", cnt1);
  }
  return 0;
}
