#include <bits/stdc++.h>
using namespace std;
long long expo(long long base, long long exponent) {
  long long ans = 1;
  while (exponent != 0) {
    if (exponent & 1) ans = (1LL * ans * base) % 1000000007;
    base = (1LL * base * base) % 1000000007;
    exponent >>= 1;
  }
  return ans % 1000000007;
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    long long ar[n];
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n))) {
      cin >> ar[i];
    }
    if (p == 1) {
      cout << ((n % 2)) << '\n';
      continue;
    }
    sort(ar, ar + n);
    reverse(ar, ar + n);
    long long ans = expo(p, ar[0]);
    long long oflag = 0;
    long long sum = 1;
    for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n));
         i += 1 - 2 * ((1) > (n))) {
      if (oflag == 1) {
        ans = (ans + 1000000007 - expo(p, ar[i])) % 1000000007;
        continue;
      }
      long long diff = ar[i - 1] - ar[i];
      for (__typeof(diff) j = (0) - ((0) > (diff));
           j != (diff) - ((0) > (diff)); j += 1 - 2 * ((0) > (diff))) {
        if (sum == 0) break;
        sum *= p;
        if (sum >= 1000001) {
          oflag = 1;
          break;
        }
      }
      if (sum == 0) {
        ans = (ans + expo(p, ar[i])) % 1000000007;
        sum = 1;
        continue;
      }
      if (sum > 0) {
        sum--;
        ans = (ans + 1000000007 - expo(p, ar[i])) % 1000000007;
        continue;
      }
    }
    cout << ans << '\n';
  }
  return 0;
}
