#include <bits/stdc++.h>
using namespace std;
long long mult(long long a, long long b, long long p = (long long)(1e9 + 7)) {
  return ((a % p) * (b % p)) % p;
}
long long add(long long a, long long b, long long p = (long long)(1e9 + 7)) {
  return (a % p + b % p) % p;
}
long long neg(long long a, long long p = (long long)(1e9 + 7)) {
  return (p - (a % p)) % p;
}
long long sub(long long a, long long b, long long p = (long long)(1e9 + 7)) {
  return add(a, neg(b));
}
long long fpow(long long x, long long y) {
  long long res = 1;
  x = x % (long long)(1e9 + 7);
  if (x == 0) return 0;
  while (y > 0) {
    if (y & 1LL) res = (res * x) % (long long)(1e9 + 7);
    y = y >> 1LL;
    x = (x * x) % (long long)(1e9 + 7);
  }
  return res;
}
long long inv(long long a, long long p = (long long)(1e9 + 7)) {
  return fpow(a, p - 2);
}
bool sa(const pair<long long, long long> &a,
        const pair<long long, long long> &b) {
  return (a.second < b.second);
}
bool fd(const pair<long long, long long> &a,
        const pair<long long, long long> &b) {
  return (a.first > b.first);
}
bool sd(const pair<long long, long long> &a,
        const pair<long long, long long> &b) {
  return (a.second > b.second);
}
long long dx[4] = {0, 0, 1, -1};
long long dy[4] = {1, -1, 0, 0};
bool valid(long long x, long long y, long long n, long long m) {
  if (x < 0 || y < 0) {
    return false;
  } else if (x >= n || y >= m) {
    return false;
  } else
    return true;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long T;
  T = 1;
  cin >> T;
  for (long long t = 1; t < T + 1; t++) {
    long long n, p, ans = 0, val = 0, ans1 = 0, z;
    set<long long> s1, s2;
    cin >> n >> p;
    vector<long long> a(n);
    map<long long, long long> mpp;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    if (p == 1) {
      cout << (n & 1) << "\n";
      continue;
    }
    sort(a.begin(), a.end(), greater<long long>());
    for (long long i = 0; i < n; i++) {
      if (s1.empty()) {
        s1.insert(a[i]);
      } else if (s2.empty()) {
        s2.insert(a[i]);
        mpp[a[i]]++;
      } else if ((*s1.rbegin()) > (*s2.rbegin())) {
        s2.insert(a[i]);
        mpp[a[i]]++;
        z = a[i];
        while (mpp[z] == p) {
          s2.erase(z);
          mpp[z] = 0;
          z++;
          mpp[z]++;
          s2.insert(z);
        }
      }
      while (!s1.empty() && !s2.empty() && (*s1.rbegin()) == (*s2.rbegin())) {
        z = (*s1.rbegin());
        s1.erase(z);
        mpp[z]--;
        if (!mpp[z]) s2.erase(*s2.rbegin());
      }
    }
    for (auto &it : s1) {
      ans = add(ans, fpow(p, it));
    }
    for (auto &it1 : s2) {
      for (long long i = 0; i < mpp[it1]; i++) {
        ans1 = add(ans1, fpow(p, it1));
      }
    }
    val = sub(ans, ans1);
    cout << val << "\n";
  }
  return 0;
}
