#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 123;
const long long mod = 1e9 + 7;
int n, p;
int a[N];
long long powerMod(long long x, long long y) {
  if (y == 0) return 1;
  if (y == 1) return x;
  long long a = powerMod(x, y >> 1);
  a = (a * a) % mod;
  return y & 1 ? a * x % mod : a;
}
long long cal(int i, long long x) {
  for (int j = i; j; --j) {
    x = (x * powerMod(p, a[j + 1] - a[j])) % mod;
    x = (x - 1) % mod;
  }
  return (x + mod) % mod;
}
void testcase() {
  cin >> n >> p;
  for (int i = 1; i <= n; ++i) cin >> a[i];
  if (p == 1) {
    cout << (n & 1) << "\n";
    return;
  }
  long long x = 1, pPower = 0;
  while (x * p <= n) {
    x *= p;
    ++pPower;
  }
  sort(a + 1, a + 1 + n);
  long long res = 1;
  for (int i = n - 1; i; --i) {
    if (res == 0) {
      res = 1;
    } else {
      int k = a[i + 1] - a[i];
      if (k > pPower) {
        res = cal(i, res);
        break;
      } else {
        long long x = powerMod(p, k);
        if (res * x > n) {
          res = cal(i, res);
          break;
        } else {
          res *= x;
          res = (res - 1);
        }
      }
    }
  }
  res = res * powerMod(p, a[1]) % mod;
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie();
  cout.tie();
  int tcase = 1;
  cin >> tcase;
  for (int i = 1; i <= tcase; ++i) testcase();
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
