#include <bits/stdc++.h>
const int N = 1e6 + 5;
using namespace std;
int T, n, p;
int k[N];
pair<long long, long long> Sum, MOD = make_pair(1e9 + 7, 1e9 + 8);
pair<long long, long long> operator+(pair<long long, long long> A,
                                     pair<long long, long long> B) {
  return make_pair((A.first + B.first) % MOD.first,
                   (A.second + B.second) % MOD.second);
}
long long Pow1(int a, int b) {
  if (b == 0) return 1;
  long long c = Pow1(a, b / 2);
  if (b % 2 == 0) return c * c % MOD.first;
  return ((c * c % MOD.first) * a) % MOD.first;
}
long long Pow2(int a, int b) {
  if (b == 0) return 1;
  long long c = Pow2(a, b / 2);
  if (b % 2 == 0) return c * c % MOD.second;
  return ((c * c % MOD.second) * a) % MOD.second;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) cin >> k[i];
    sort(k + 1, k + n + 1);
    for (int i = n; i >= 1; i--) {
      Sum = make_pair(Pow1(p, k[i]), Pow2(p, k[i]));
      for (int j = i - 1; j >= 0; j--) {
        if (j == 0) {
          i = 0;
          break;
        }
        Sum = Sum + make_pair((-Pow1(p, k[j]) + MOD.first) % MOD.first,
                              (-Pow2(p, k[j]) + MOD.second) % MOD.second);
        if (Sum == make_pair(0ll, 0ll)) {
          i = j;
          break;
        }
      }
    }
    cout << Sum.first << '\n';
  }
}
