#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
const long long ML = 4e18;
int n;
long long Pow(long long p, long long x) {
  long long ans = 1;
  while (x) {
    if (x & 1) ans = (ans * p) % 1000000007;
    x >>= 1;
    p = (p * p) % 1000000007;
  }
  return ans;
}
long long Pow1(long long p, long long x) {
  long long ans = 1;
  while (x) {
    if (x & 1) ans = (ans * p);
    x >>= 1;
    p = (p * p);
  }
  return ans;
}
void solve() {
  long long n, p;
  cin >> n >> p;
  vector<long long> v(n), count(1e6 + 102, 0);
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
    count[v[i]]++;
  }
  if (p == 1) {
    cout << n % 2 << endl;
    return;
  }
  v.push_back(0);
  sort(v.begin(), v.end());
  int sz = unique(v.begin(), v.end()) - v.begin();
  int flag = 0;
  long long cur = 0, cnt = 0;
  double x = 1.0;
  while (x < 1e18) {
    x *= p;
    cnt++;
  }
  for (int i = sz - 1; i >= 1; --i) {
    if (flag == 0) {
      if (count[v[i]] % 2 == 0)
        continue;
      else {
        int d = v[i] - v[i - 1];
        if (d >= cnt) {
          flag = -1;
          cur = Pow(p, v[i]);
        } else {
          flag = 1;
          cur = Pow1(p, d);
        }
      }
    } else if (flag == 1) {
      if (count[v[i]] >= cur) {
        long long p = count[v[i]] - cur;
        if (p % 2 == 0)
          flag = 0;
        else {
          int d = v[i] - v[i - 1];
          if (d >= cnt) {
            flag = -1;
            cur = Pow(p, v[i]);
          } else {
            flag = 1;
            cur = Pow1(p, d);
          }
        }
      } else {
        cur -= count[i];
        long long d = v[i] - v[i - 1];
        if (d >= cnt) {
          flag = -1;
          cur = (cur % 1000000007 * Pow(p, v[i]) % 1000000007) % 1000000007;
          continue;
        }
        long long x = Pow1(p, d);
        double mx = cur * 1.0 * x;
        if (mx > 1e18) {
          flag = -1;
          cur = ((cur % 1000000007) * Pow(p, v[i])) % 1000000007;
        } else
          cur *= x;
      }
    } else {
      cur = (cur - (count[v[i]] * Pow(p, v[i])) % 1000000007 + 1000000007) %
            1000000007;
    }
  }
  if (flag == 0)
    cur = count[0] % 2;
  else if (flag == 1) {
    if (count[0] >= cur)
      cur = (count[0] - cur) % 2;
    else
      cur -= count[0];
  } else {
    cur = (cur - count[0] + 1000000007) % 1000000007;
  }
  cout << cur << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T;
  cin >> T;
  for (int kase = 1; kase <= T; ++kase) {
    solve();
  }
  return 0;
}
