#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int N = 1000005;
int Pow(int a, int b) {
  int res = 1;
  for (; b > 0; b >>= 1) {
    if (b & 1) res = 1LL * res * a % MOD;
    a = 1LL * a * a % MOD;
  }
  return res;
}
int n, p;
int a[N];
map<int, int> mp;
void del(int x) {
  auto it = mp.lower_bound(x);
  int id = it->first;
  mp[id]--;
  if (mp[id] == 0) mp.erase(id);
  mp[x] += Pow(p, id - x) - 1;
  while (mp[x] >= p) {
    mp[x + 1] += mp[x] / p;
    mp[x] %= p;
    if (mp[x] == 0) mp.erase(x);
    x++;
  }
  if (mp[x] == 0) mp.erase(x);
}
void show() {
  cout << "--" << '\n';
  for (auto i : mp) {
    cout << i.first << " " << i.second << '\n';
  }
  cout << "--" << '\n';
  cout << flush;
}
map<int, int> mp2;
void add(int x) {
  mp2[x]++;
  while (mp2[x] >= p) {
    mp2[x + 1] += mp2[x] / p;
    mp2[x] %= p;
    if (mp2[x] == 0) mp2.erase(x);
    x++;
  }
}
int main() {
  ios_base ::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> p;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    if (p == 1) {
      cout << n % 2 << '\n';
      continue;
    }
    if (n == 1) {
      cout << Pow(p, a[1]) << '\n';
      continue;
    }
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    mp.clear();
    for (int i = n; i >= 1; i--) {
      mp[a[i]]++;
      int x = a[i];
      while (mp[x] >= p) {
        mp[x + 1] += mp[x] / p;
        mp[x] %= p;
        if (mp[x] == 0) mp.erase(x);
        x++;
      }
    }
    int res = 0;
    for (int i = 1; i <= n;) {
      del(a[i]);
      int id;
      if (!mp.empty() && (--mp.end())->first >= a[i]) {
        int j = i + 1;
        while (j <= n) {
          del(a[j]);
          add(a[j]);
          j++;
          if (mp2[a[i]] == 1) {
            mp2.clear();
            break;
          }
        }
        i = j;
      } else {
        res = Pow(p, a[i]);
        for (int j = i + 1; j <= n; j++) {
          res = (res - Pow(p, a[j]) + MOD) % MOD;
        }
        break;
      }
    }
    cout << res << '\n';
  }
  return 0;
}
