#include <bits/stdc++.h>
using namespace std;
long long int power(int a, int b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2) {
    long long int x = power(a, b / 2);
    return (((x * x) % 1000000007) * a) % 1000000007;
  } else {
    long long int x = power(a, b / 2);
    return (x * x) % 1000000007;
  }
}
int howmany(int v, int n, int p) {
  if (v == 0) {
    return 100000000;
  }
  int ans = 0;
  while (v < n) {
    v *= p;
    ans++;
  }
  return ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t, n, p;
  cin >> t;
  while (t--) {
    cin >> n >> p;
    int arr[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    int v = 0;
    long long int sumi = 0;
    sort(arr, arr + n);
    reverse(arr, arr + n);
    if (p == 1) {
      if (n % 2) {
        cout << 1 << "\n";
      } else {
        cout << "0\n";
      }
      continue;
    }
    sumi = power(p, arr[0]);
    v = 1;
    for (int i = 1; i < n; i++) {
      int powmul = arr[i - 1] - arr[i];
      if (powmul >= howmany(v, n, p)) {
        for (int j = i; j < n; j++) {
          sumi -= power(p, arr[j]);
          if (sumi < 0) {
            sumi += 1000000007;
          }
        }
        break;
      }
      if (v > 0) {
        v = v * pow(p, powmul) - 1;
        sumi -= power(p, arr[i]);
        if (sumi < 0) {
          sumi += 1000000007;
        }
      } else {
        sumi += power(p, arr[i]);
        if (sumi >= 1000000007) {
          sumi -= 1000000007;
        }
        v++;
      }
    }
    cout << sumi << "\n";
  }
}
