#include <bits/stdc++.h>
using namespace std;
long long m(long long a, long long b) {
  if (a >= 0) {
    return a % b;
  } else {
    return m((a % b + b), b);
  }
}
long long get(long long a, long long b) {
  long long ans = 1ll;
  if (b == 0) {
    return 1;
  }
  while (b != 1) {
    if (b % 2 == 0) {
      a = (a * a) % 1000000007;
      b = b / 2;
    } else {
      ans = (ans * a) % 1000000007;
      b--;
    }
  }
  return (ans * a) % 1000000007;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p;
    scanf("%d %d", &n, &p);
    vector<int> a;
    a.resize(n);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    sort(a.begin(), a.end());
    vector<int> v;
    v.resize(n);
    int ind = n - 2;
    long long s = 1;
    v[n - 1] = 1;
    while (ind >= 0) {
      if (s == 0) {
        s = 1;
        v[ind] = 1;
        ind--;
      } else if (s < 0) {
        if (log(-1 * s) + (a[ind + 1] - a[ind]) * log(p) > log(n)) {
          for (int l = 0; l <= ind; l++) {
            v[l] = 1;
          }
          break;
        } else {
          s = s * (long long)(pow(p, (a[ind + 1] - a[ind]))) + 1;
          v[ind] = 1;
          ind--;
        }
      } else {
        if ((log(s) + (a[ind + 1] - a[ind]) * log(p) > log(n))) {
          for (int l = 0; l <= ind; l++) {
            v[l] = -1;
          }
          break;
        } else {
          s = s * (long long)(pow(p, (a[ind + 1] - a[ind]))) - 1;
          v[ind] = -1;
          ind--;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      a[i] = get(p, a[i]);
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      if (v[i] == 1) {
        ans = m((ans + a[i]), 1000000007);
      } else {
        ans = m((ans - a[i]), 1000000007);
      }
    }
    cout << ans << endl;
  }
}
