#include <bits/stdc++.h>
using namespace std;
long long mod(long long n) {
  return (n % (long long)1000000007 + (long long)1000000007) %
         (long long)1000000007;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
long long binpow(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = mod(res * a);
    a = mod(a * a);
    b >>= 1;
  }
  return res;
}
long long fc(long long p, long long d, long long n, long long preq) {
  if (p == 1) return 1;
  long long req = 1;
  for (long long i = 1; i < d + 1; i = i + 1) {
    if (req * p > n) return -1;
    req *= p;
  }
  if (req * preq > n) return -1;
  return req * preq;
}
vector<long long> f(1000005);
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> a;
    for (long long i = 0; i < n; i = i + 1) {
      long long x;
      cin >> x;
      f[x]++;
      if (f[x] == 1) a.push_back(x);
    }
    if (p == 1) {
      cout << (n & 1) << "\n";
      for (long long i = 0; i < n; i = i + 1) f[a[i]] = 0;
      continue;
    }
    int old_n = n;
    n = a.size();
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    long long l = 0;
    long long ans = 0;
    while (true) {
      if (l >= n) break;
      if (f[a[l]] % 2 == 0) {
        l++;
        continue;
      }
      long long preq = 1;
      long long old = a[l];
      long long flag = 0;
      long long req;
      for (long long r = l + 1; r < n; r = r + 1) {
        req = fc(p, old - a[r], old_n, preq);
        if (req == -1) {
          break;
        } else {
          if (req > f[a[r]]) {
            old = a[r];
            preq = req - f[a[r]];
          } else {
            f[a[r]] -= req;
            flag = 1;
            l = r;
            break;
          }
        }
      }
      if (flag == 0) {
        ans = binpow(p, a[l]);
        for (long long r = l + 1; r < n; r = r + 1) {
          ans = mod(ans - mod(binpow(p, a[r]) * f[a[r]]));
        }
        break;
      }
    }
    cout << ans << "\n";
    for (long long i = 0; i < n; i = i + 1) f[a[i]] = 0;
  }
}
