#include <bits/stdc++.h>
using namespace std;
const long long mod1 = 1000000007;
const long long mod2 = 10995514;
const long long mod3 = 1099551473989;
long long k[1000005];
long long quick_pow(long long a, long long b, long long mod) {
  if (a == 0) {
    return 0;
  }
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long n, p;
    scanf("%lld%lld", &n, &p);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &k[i]);
    }
    sort(k + 1, k + 1 + n, greater<int>());
    long long ans1 = 0, ans2 = 0, ans3 = 0, sig = 0;
    for (int i = 1; i <= n; i++) {
      sig = -1;
      if (ans1 == 0 && ans2 == 0 && ans3 == 0) {
        sig = 1;
      }
      ans1 = (ans1 + quick_pow(p, k[i], mod1) * sig) % mod1;
      ans2 = (ans2 + quick_pow(p, k[i], mod2) * sig) % mod2;
      ans3 = (ans3 + quick_pow(p, k[i], mod3) * sig) % mod3;
    }
    cout << ans1 << endl;
  }
  return 0;
}
