#include <bits/stdc++.h>
using namespace std;
bool desc_sort(const int& lhs, const int& rhs) { return lhs > rhs; }
bool desc_pii_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  return lhs > rhs;
}
bool second_sort(const pair<int, int>& lhs, const pair<int, int>& rhs) {
  pair<int, int> a, b;
  a.first = lhs.second;
  a.second = lhs.first;
  b.first = rhs.second;
  b.second = rhs.first;
  return a < b;
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class T>
ostream& operator<<(ostream& os, set<T> second) {
  os << "{ ";
  for (auto s : second) os << s << " ";
  return os << "}";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R>
ostream& operator<<(ostream& os, map<L, R> M) {
  os << "{ ";
  for (auto m : M) os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << "\n";
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
inline long long fast_expo(long long base, long long power,
                           long long modulo = 1000000007) {
  base %= modulo;
  if (base < 0LL) base += modulo;
  long long x = base, cnt = power, ans = 1LL;
  while (cnt) {
    if (cnt & 1LL) ans = (ans * x) % modulo;
    x = (x * x) % modulo;
    cnt >>= 1LL;
  }
  return ans;
}
inline long long inv(long long base, long long modulo = 1000000007) {
  return fast_expo(base, modulo - 2LL, modulo);
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    cin >> n >> p;
    int a[n];
    for (int i = 0; i < n; i++) cin >> a[i];
    if (p == 1) {
      if (n & 1)
        cout << (1) << "\n";
      else
        cout << (0) << "\n";
      continue;
    }
    sort(a, a + n);
    vector<pair<int, int> > b;
    int cc = 1;
    for (int i = 1; i < n; i++) {
      if (a[i] == a[i - 1])
        cc++;
      else {
        b.push_back({a[i - 1], cc});
        cc = 1;
      }
    }
    b.push_back({a[n - 1], cc});
    bool max_k[(b).size() + 1];
    cc = 0;
    for (int i = 0; i < (b).size() - 1; i++) {
      cc += b[i].second;
      int r = b[i].first;
      while (r < b[i + 1].first and cc > 0) {
        r++;
        cc = cc / p;
      }
      if (r == b[i + 1].first and cc > 0)
        max_k[i] = 1;
      else
        max_k[i] = 0;
    }
    bool found = 0;
    long long ans;
    for (int i = (b).size() - 1; i >= 1; i--) {
      b[i].second = (b[i].second) & 1;
      if (b[i].second) {
        if (max_k[i - 1]) {
          int need = fast_expo(p, b[i].first - b[i - 1].first);
          int j = i - 1;
          while (need > 0) {
            if (need <= b[j].second) {
              b[j].second -= need;
              break;
            } else {
              need -= b[j].second;
              b[j].second = 0;
              need = need * fast_expo(p, b[j].first - b[j - 1].first);
              j--;
            }
          }
        } else {
          found = 1;
          ans = fast_expo(p, b[i].first);
          for (int j = i - 1; j >= 0; j--) {
            long long temp;
            temp = fast_expo(p, b[j].first);
            temp *= (long long)b[j].second;
            temp = temp % 1000000007;
            ans = (ans - temp + 1000000007) % 1000000007;
          }
          break;
        }
      }
    }
    if (found)
      cout << (ans) << "\n";
    else {
      if ((b[0].second) & 1) {
        cout << (fast_expo(p, b[0].first)) << "\n";
      } else
        cout << (0) << "\n";
    }
  }
  return 0;
}
