#include <bits/stdc++.h>
using namespace std;
long long p, pt;
priority_queue<long long> digit_que;
void div(map<long long, long long>& k, map<long long, bool>& isjin) {
  while (!digit_que.empty()) {
    pt = digit_que.top();
    digit_que.pop();
    if (k[pt] & 1) {
      if (!isjin[pt]) {
        k[pt] = 1;
        return;
      } else if (p % 2 == 0) {
        k[pt] = 0;
        continue;
      } else {
        k[pt] = 0;
        k[pt - 1]++;
        digit_que.push(pt - 1);
      }
    }
    k[pt] = 0;
  }
  pt = -1;
}
long long fpow(long long a, long long t) {
  long long ans = 1, tmp = a;
  while (t > 0) {
    if (t & 1) ans = ans * tmp % 1000000007LL;
    tmp = tmp * tmp % 1000000007LL;
    t >>= 1;
  }
  return ans;
}
int main() {
  long long t, n, dig;
  cin.sync_with_stdio(0);
  cin.tie(0);
  cin >> t;
  while (t-- > 0) {
    map<long long, long long> k;
    map<long long, bool> isjin;
    cin >> n >> p;
    if (p == 1) {
      cout << (n % 2) << endl;
      for (int i = 0; i < n; i++) cin >> dig;
      continue;
    }
    for (int i = 0; i < n; i++) {
      cin >> dig;
      k[dig]++;
      digit_que.push(dig);
      while (k[dig] >= p) {
        k[dig + 1] = k[dig + 1] + 1;
        digit_que.push(dig + 1);
        isjin[dig + 1] = true;
        k[dig] = 0;
        dig++;
      }
    }
    div(k, isjin);
    if (pt == -1) {
      cout << 0 << '\n';
      while (!digit_que.empty()) digit_que.pop();
      continue;
    }
    long long less = 0, more = fpow(p, pt), rem = pt;
    while (!digit_que.empty()) {
      if (digit_que.top() == rem) {
        digit_que.pop();
        continue;
      }
      rem = digit_que.top();
      less = (less + k[rem] * fpow(p, rem)) % 1000000007LL;
      digit_que.pop();
    }
    cout << (more + 1000000007LL - less) % 1000000007LL << '\n';
  }
  return 0;
}
