#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
template <typename first, typename second>
ostream& operator<<(ostream& os, const pair<first, second>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "{";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "}";
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ",";
    os << *it;
  }
  return os << "]";
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << *it;
  }
  return os << "]";
}
template <typename first, typename second>
ostream& operator<<(ostream& os, const map<first, second>& v) {
  os << "[";
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << ", ";
    os << it->first << " = " << it->second;
  }
  return os << "]";
}
void faltu() { cerr << '\n'; }
template <typename T>
void faltu(T a[], int n) {
  for (int i = 0; i < n; ++i) cerr << a[i] << ' ';
  cerr << '\n';
}
template <typename T, typename... hello>
void faltu(T arg, const hello&... rest) {
  cerr << arg << ' ';
  faltu(rest...);
}
long long expo(long long b, long long p, long long m) {
  long long res = 1LL, x = b % m;
  while (p) {
    if (p & 1LL) res = (res * x) % m;
    x = (x * x) % m;
    p >>= 1LL;
  }
  return res;
}
const int mx = 1e6 + 5;
long long n, p;
long long k[mx];
deque<pair<long long, long long> > vec1, vec2;
deque<pair<long long, long long> > store;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int t;
  cin >> t;
  while (t--) {
    vec1.clear(), vec2.clear();
    store.clear();
    cin >> n >> p;
    for (int i = 0; i < n; ++i) {
      cin >> k[i];
    }
    if (p == 1) {
      if (n % 2 == 1)
        cout << 1 << '\n';
      else
        cout << 0 << '\n';
      continue;
    }
    sort(k, k + n, greater<long long>());
    int konta = 1;
    for (int i = 0; i < n; ++i) {
      if (konta == 1) {
        store.clear();
        vec1.push_back(make_pair(k[i], 1));
        while (1) {
          bool ok = 0;
          if ((int)vec1.size() > 1) {
            pair<long long, long long> p1 = vec1[(int)vec1.size() - 2];
            pair<long long, long long> p2 = vec1[(int)vec1.size() - 1];
            if (p1.first == p2.first) {
              p1.second += p2.second;
              vec1.pop_back();
              vec1.pop_back();
              vec1.push_back(p1);
              ok = 1;
            }
          }
          if (ok) continue;
          if (!vec1.empty()) {
            pair<long long, long long> p1 = vec1[(int)vec1.size() - 1];
            if (p1.second >= p) {
              if (p1.second % p)
                store.push_back(make_pair(p1.first, p1.second % p));
              vec1.pop_back();
              vec1.push_back(make_pair(p1.first + 1, p1.second / p));
              ok = 1;
            }
          }
          if (!ok) break;
        }
        while (!store.empty()) {
          vec1.push_back(store.back());
          store.pop_back();
        }
      } else if (konta == 2) {
        store.clear();
        vec2.push_back(make_pair(k[i], 1));
        while (1) {
          bool ok = 0;
          if ((int)vec2.size() > 1) {
            pair<long long, long long> p1 = vec2[(int)vec2.size() - 2];
            pair<long long, long long> p2 = vec2[(int)vec2.size() - 1];
            if (p1.first == p2.first) {
              p1.second += p2.second;
              vec2.pop_back();
              vec2.pop_back();
              vec2.push_back(p1);
              ok = 1;
            }
          }
          if (ok) continue;
          if (!vec2.empty()) {
            pair<long long, long long> p1 = vec2[(int)vec2.size() - 1];
            if (p1.second >= p) {
              if (p1.second % p)
                store.push_back(make_pair(p1.first, p1.second % p));
              vec2.pop_back();
              vec2.push_back(make_pair(p1.first + 1, p1.second / p));
              ok = 1;
            }
          }
          if (!ok) break;
        }
        while (!store.empty()) {
          vec2.push_back(store.back());
          store.pop_back();
        }
      }
      while (1) {
        if (vec1.empty()) break;
        if (vec2.empty()) break;
        if (vec1.front() == vec2.front()) {
          vec1.pop_front();
          vec2.pop_front();
        } else
          break;
      }
      if (vec1.empty()) {
        konta = 1;
        continue;
      }
      if (vec2.empty()) {
        konta = 2;
        continue;
      }
      if (vec1[0].first < vec2[0].first)
        konta = 1;
      else if (vec1[0].first > vec2[0].first)
        konta = 2;
      else if (vec1[0].first == vec2[0].first) {
        if (vec1[0].second <= vec2[0].second)
          konta = 1;
        else
          konta = 2;
      }
    }
    long long a = 0, b = 0;
    for (auto x : vec1) {
      long long temp = (x.second * expo(p, x.first, 1000000007)) % 1000000007;
      a = (a + temp) % 1000000007;
    }
    for (auto x : vec2) {
      long long temp = (x.second * expo(p, x.first, 1000000007)) % 1000000007;
      b = (b + temp) % 1000000007;
    }
    long long ans = (a - b + 1000000007) % 1000000007;
    cout << ans << '\n';
  }
}
