#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323846;
const long long INF = 9223372036854775807;
const long long mod = 1000000007;
long long fastPow(long long b, long long e) {
  long long r = 1;
  while (e) {
    if (e % 2 == 1) {
      r *= b;
      r %= mod;
    }
    b *= b;
    b %= mod;
    e /= 2;
  }
  return r;
}
long long fastPow2(long long b, long long e) {
  long long r = 1;
  while (e) {
    if (e % 2 == 1) {
      r *= b;
      if (r > 1000000) r = 1000000;
    }
    b *= b;
    if (b > 1000000) b = 1000000;
    e /= 2;
  }
  return r;
}
long long pgcd(long long a, long long b) {
  if (a % b == 0)
    return b;
  else
    return pgcd(b, a % b);
}
long long sign(long long a) {
  if (a < 0) {
    return -1;
  }
  if (a == 0) {
    return 0;
  }
  return 1;
}
bool isPrime(long long a) {
  if (a == 1) {
    return false;
  }
  long long f = sqrt(a);
  for (long long i = 2; i <= f; i++) {
    if (a % i == 0) {
      return false;
    }
  }
  return true;
}
long long toInt(string s) {
  long long tot = 0;
  for (long long i = s.size() - 1; i >= 0; i--) {
    tot += ((s[i] - '0') % mod) * fastPow(10, i);
    tot %= mod;
  }
  return tot;
}
string toString(long long a) {
  string s = "";
  while (a) {
    s = (char)('0' + a % 10) + s;
    a /= 10;
  }
  return s;
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p;
    cin >> n >> p;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) {
      cin >> v[i];
    }
    if (p == 1) {
      cout << n % 2 << endl;
      continue;
    }
    sort(v.begin(), v.end());
    long long num = 0;
    long long last = 0;
    long long A = 0;
    long long B = 0;
    while (!v.empty()) {
      if (num == 0) {
        last = v.back();
        num = 1;
        v.pop_back();
        A += fastPow(p, last);
        if (A >= mod) A -= mod;
      } else {
        if (last != v.back()) {
          num *= fastPow2(p, (last - v.back()));
          if (num >= n) num = n;
          last = v.back();
        }
        B += fastPow(p, v.back());
        if (B >= mod) B -= mod;
        v.pop_back();
        num--;
      }
    }
    cout << (A - B + mod) % mod << endl;
  }
}
