#include <bits/stdc++.h>
using namespace std;
namespace input {
template <class T>
void scan(T& x) {
  cin >> x;
}
template <class T>
void scan_iter(const T& begin, const T& end) {
  for (T it = begin; it != end; it++) {
    scan(*it);
  }
}
template <class T, class... Ts>
void scan(T& t, Ts&... ts) {
  scan(t);
  scan(ts...);
}
template <class T1, class T2>
void scan(pair<T1, T2>& p) {
  scan(p.first, p.second);
}
template <class T>
void scan(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) scan(a[i]);
}
template <class T>
void cscan(T& x) {}
void cscan(int& x) { scanf("%d", &x); }
void cscan(long long& x) { scanf("%lld", &x); }
void cscan(double& x) { scanf("%lf", &x); }
void cscan(char* x) { scanf("%s", x); }
template <class T>
void cscan_iter(const T& begin, const T& end) {
  for (T it = begin; it != end; it++) {
    cscan(*it);
  }
}
template <class T, class... Ts>
void cscan(T& t, Ts&... ts) {
  cscan(t);
  cscan(ts...);
}
template <class T1, class T2>
void cscan(pair<T1, T2>& p) {
  cscan(p.first, p.second);
}
template <class T>
void cscan(vector<T>& a) {
  for (int i = 0; i < a.size(); i++) cscan(a[i]);
}
}  // namespace input
using namespace input;
namespace output {
void print(int x) { cout << x; }
void print(size_t x) { cout << x; }
void print(long long x) { cout << x; }
void print(double x) { cout << x; }
void print(const char* x) { cout << x; }
void print(const string& x) { cout << x; }
template <class T, class... Ts>
void print(const T& t, const Ts&... ts) {
  print(t);
  if (sizeof...(ts)) print(" ");
  print(ts...);
}
template <class T1, class T2>
void print(const pair<T1, T2>& x) {
  print(x.first, x.second);
}
template <class T>
void print_iter(const T& begin, const T& end) {
  bool first = true;
  for (T it = begin; it != end; it++) {
    if (!first) print(" ");
    print(*it);
    first = false;
  }
}
template <class T>
void print(const initializer_list<T>& x) {
  print_iter(x.begin(), x.end());
}
template <class T>
void print(const vector<T>& x) {
  print_iter(x.begin(), x.end());
}
void print() { print("\n"); }
template <class T>
void println(const T& x) {
  print(x);
  print();
}
void cprint(int x) { printf("%d", x); }
void cprint(size_t x) { printf("%zd", x); }
void cprint(long long x) { printf("%lld", x); }
void cprint(double x) { printf("%lf", x); }
void cprint(char* x) { printf("%s", x); }
void cprint(const char* x) { printf("%s", x); }
void cprint(const string& x) { cprint(x.c_str()); }
template <class T, class... Ts>
void cprint(const T& t, const Ts&... ts) {
  cprint(t);
  if (sizeof...(ts)) cprint(" ");
  cprint(ts...);
}
template <class T1, class T2>
void cprint(const pair<T1, T2>& x) {
  cprint(x.first, x.second);
}
template <class T>
void cprint_iter(const T& begin, const T& end) {
  bool first = true;
  for (T it = begin; it != end; it++) {
    if (!first) cprint(" ");
    cprint(*it);
    first = false;
  }
}
template <class T>
void cprint(const initializer_list<T>& x) {
  cprint_iter(x.begin(), x.end());
}
template <class T>
void cprint(const vector<T>& x) {
  cprint_iter(x.begin(), x.end());
}
void cprint() { cprint("\n"); }
template <class T>
void cprintln(const T& x) {
  cprint(x);
  cprint();
}
}  // namespace output
using namespace output;
const int maxn = 1000000 + 5;
const long long mod = 1000000000 + 7;
long long k[maxn];
long long pow_mod(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    b >>= 1;
    a = a * a % mod;
  }
  return res;
}
int main(void) {
  int t;
  cscan(t);
  while (t--) {
    int n;
    long long p;
    cscan(n, p);
    cscan_iter(k + 1, k + n + 1);
    if (n == 1) {
      cprintln(pow_mod(p, k[1]));
    } else {
      sort(k + 1, k + n + 1);
      reverse(k + 1, k + n + 1);
      long long left = -1;
      long long cnt = 0;
      bool finish = false;
      for (int i = 1; i <= n;) {
        int j = i;
        for (; j <= n && k[j] == k[i]; j++)
          ;
        long long right = k[i];
        long long cnt2 = j - i;
        if (cnt != 0) {
          if (log(cnt) + (left - right) * log(p) > log(n)) {
            finish = true;
          } else {
            cnt *= pow_mod(p, left - right);
            left = right;
          }
          if (finish) {
            long long val1 = pow_mod(p, left) * (cnt % mod) % mod;
            long long val2 = 0;
            while (i <= n) {
              val2 = (val2 + pow_mod(p, k[i])) % mod;
              i++;
            }
            cprintln((val1 - val2 + mod) % mod);
            break;
          }
        }
        i = j;
        if (cnt > cnt2) {
          cnt -= cnt2;
        } else {
          cnt2 -= cnt;
          if (cnt2 % 2 == 1) {
            left = right;
            cnt = 1;
          } else {
            cnt = 0;
          }
        }
      }
      if (!finish) {
        if (cnt == 0) {
          cprintln(0);
        } else {
          cprintln(pow_mod(p, left) * (cnt % mod) % mod);
        }
      }
    }
  }
  return 0;
}
