#include <bits/stdc++.h>
using namespace std;
const int N = 1000007;
const int MOD = 1000000007;
int freq[N];
int a[N];
int cal(int x, int p, int y) {
  if (p == 1) return x;
  int ans = 1;
  for (int i = 1; i <= y; i++) {
    if (1LL * ans * p > N) return -1;
    ans *= p;
  }
  if (1LL * x * ans > N)
    return -1;
  else
    return x * ans;
}
int pow_log(int x, int p) {
  int ans = 1;
  while (p) {
    if (p % 2) {
      ans = (1LL * ans * x) % MOD;
      p--;
    } else {
      x = (1LL * x * x) % MOD;
      p >>= 1;
    }
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int a1;
  cin >> a1;
  for (int f = 0; f < a1; f++) {
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      freq[a[i]]++;
    }
    sort(a, a + n, greater<int>());
    int new_n = 1;
    for (int i = 1; i < n; i++) {
      if (a[i] != a[i - 1]) a[new_n++] = a[i];
    }
    n = new_n;
    int dif = 0;
    for (int i = 0; i < n; i++) {
      if (freq[a[i]] % 2 == 0) continue;
      int x = a[i];
      int need = 1, good = 0;
      for (int j = i + 1; j < n; j++) {
        int val = cal(need, k, (x - a[j]));
        if (val == -1) break;
        if (val <= freq[a[j]]) {
          freq[a[j]] -= val;
          i = j;
          good = 1;
          break;
        } else {
          x = a[j];
          need = val - freq[a[j]];
        }
      }
      if (!good) {
        dif = pow_log(k, a[i]);
        for (int j = i + 1; j < n; j++) {
          dif -= (1LL * freq[a[j]] * pow_log(k, a[j])) % MOD;
          if (dif < 0) {
            dif += MOD;
          }
        }
        break;
      } else
        i--;
    }
    for (int i = 0; i < n; i++) freq[a[i]] = 0;
    cout << dif << "\n";
  }
  return 0;
}
