#include <bits/stdc++.h>
using namespace std;
istream &operator>>(istream &is, __int128 &vector_i) {
  int u;
  is >> u;
  vector_i = u;
  return is;
}
inline ostream &operator<<(ostream &os, __int128 &pair_i) {
  os << (int)pair_i;
  return os;
}
vector<int> g[500005];
long long binpow(long long a, long long n) {
  if (n == 0) return 1;
  if (n % 2 == 1)
    return ((binpow(a, n - 1) % 1000000007) * a) % 1000000007;
  else {
    long long b = binpow(a, n / 2) % 1000000007;
    return (b * b) % 1000000007;
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    int x;
    set<int> st;
    bool fl = false;
    map<int, int> cntt;
    vector<int> f;
    for (int i = 0; i < n; ++i) {
      cin >> x;
      st.insert(x);
      cntt[x]++;
    }
    vector<int> cnt;
    f.reserve(st.size());
    for (auto &i : st) {
      f.push_back(i);
      cnt.push_back(cntt[i]);
    }
    if (!(p - 1)) {
      cout << n % 2 << '\n';
      continue;
    }
    int y = st.size() - 1;
    long long ans = 0, c;
    bool fffl = false;
    int k, nwk;
    for (; y >= 0; --y) {
      if (fffl) {
        int nw = f[y];
        ans -= ((binpow(p, nw) % 1000000007) * cnt[y]) % 1000000007;
        if (ans < 0) {
          ans += 1000000007;
        }
      } else {
        if (!fl) {
          int nw = f[y];
          if (cnt[y] % 2) {
            fl = true;
            c = 1;
            ans = binpow(p, nw) % 1000000007;
            if (ans < 0) {
              ans += 1000000007;
            }
            k = nw;
            nwk = k;
          }
        } else {
          int nw = f[y];
          while (nwk != nw) {
            if (c * p <= 1000000) {
              c *= p;
              --nwk;
            } else {
              fffl = true;
              break;
            }
          }
          if (fffl) {
            ans -= ((binpow(p, nw) % 1000000007) * cnt[y]) % 1000000007;
            if (ans < 0) {
              ans += 1000000007;
            }
          } else {
            if (cnt[y] >= c) {
              cnt[y] -= c;
              ans = 0;
              fl = false;
              c = 0;
              k = nw;
              nwk = k;
              if (cnt[y] % 2) {
                fl = true;
                c = 1;
                ans = binpow(p, nw) % 1000000007;
                if (ans < 0) {
                  ans += 1000000007;
                }
              }
            } else {
              ans -= ((binpow(p, nw) % 1000000007) * cnt[y]) % 1000000007;
              if (ans < 0) {
                ans += 1000000007;
              }
              c -= cnt[y];
            }
          }
        }
      }
    }
    if (ans < 0) {
      return -1;
    }
    if (ans < 0) {
      ans %= 1000000007;
    }
    if (ans < 0) {
      ans += 1000000007;
    }
    cout << ans << '\n';
  }
  return 0;
}
