#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void read(T &x) {
  x = 0;
  char c = getchar();
  bool flag = false;
  while (!isdigit(c)) {
    if (c == '-') flag = true;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  if (flag) x = -x;
}
int T, n, m;
int a[1000010];
pair<long long, long long> ans;
bool cmp(const int &x, const int &y) { return x > y; }
long long qp(long long x, long long y, long long p) {
  long long v = 1;
  while (y) {
    if (y & 1) v = v * x % p;
    x = x * x % p, y >>= 1;
  }
  return v;
}
int main() {
  read(T);
  while (T--) {
    read(n), read(m), ans.first = ans.second = 0;
    for (int i = 1; i <= n; ++i) read(a[i]);
    sort(a + 1, a + n + 1, cmp);
    for (int i = 1; i <= n; ++i) {
      if (!ans.first && !ans.second) {
        ans.first = (ans.first + qp(m, a[i], 1000000007)) % 1000000007;
        ans.second = (ans.second + qp(m, a[i], 19260817)) % 19260817;
      } else {
        ans.first =
            (ans.first - qp(m, a[i], 1000000007) + 1000000007) % 1000000007;
        ans.second = (ans.second - qp(m, a[i], 19260817) + 19260817) % 19260817;
      }
    }
    printf("%lld\n", ans.first);
  }
  return 0;
}
