#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int n, p;
void push(int k, vector<pair<int, int> > &v) {
  v.push_back({k, 1});
  while (v.size() >= 2 && v[v.size() - 1].first == v[v.size() - 2].first) {
    int val = v.back().first;
    int cnt = v.back().second;
    v.pop_back();
    if (v.size() == 1 && cnt == v.back().second) {
      v.pop_back();
      break;
    }
    cnt += v.back().second;
    v.pop_back();
    if (cnt == p) {
      val++;
      cnt = 1;
    }
    v.push_back({val, cnt});
  }
}
long long int pwr(long long int a, long long int b) {
  long long int res = 1;
  while (b > 0) {
    if (b & 1) res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
long long int sub(long long int a, long long int b) {
  return (a - b + mod) % mod;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    cin >> n >> p;
    vector<int> k(n);
    for (int i = 0; i < n; i++) cin >> k[i];
    if (p == 1) {
      int ans = n & 1;
      cout << ans << '\n';
      continue;
    }
    sort(k.begin(), k.end(), greater<int>());
    vector<pair<int, int> > v;
    for (int i = 0; i < n; i++) {
      if (v.size() == 0) {
        v.push_back({k[i], 1});
      } else {
        push(k[i], v);
      }
    }
    if (v.size() == 0) {
      cout << 0 << '\n';
    } else {
      long long int ans = pwr(p, v[0].first);
      for (int i = 1; i < v.size(); i++) {
        ans = sub(ans, (pwr(p, v[i].first) * v[i].second) % mod);
      }
      cout << ans << '\n';
    }
  }
}
