#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int M = 5e5;
const long long mod = 1e9 + 7;
const long long mod1 = 998244353;
const long long mod2 = 999994537;
const int inf = 1e9 + 3;
const long long INF = 1e15;
const int dx[8] = {1, 2, 2, 1, -1, -2, -2, -1};
const int dy[8] = {2, 1, -1, -2, -2, -1, 1, 2};
void data() {}
int T, n, was[N];
long long p, k[N], ans, ans1, ans2;
vector<long long> v;
stack<pair<int, int> > a;
long long add(long long a, long long b, long long md) {
  return (a + b + md + md + md + md) % md;
}
long long mul(long long a, long long b, long long md) { return (a * b) % md; }
long long binpow(long long a, long long n, long long md) {
  if (n == 0) return 1;
  long long res = 1;
  while (n) {
    if (n & 1) res = mul(res, a, md), n--;
    a = mul(a, a, md);
    n /= 2;
  }
  return res;
}
int main() {
  data();
  scanf("%d", &T);
  while (T--) {
    scanf("%d %lld", &n, &p);
    for (int i = 1; i <= n; ++i) {
      scanf("%lld", &k[i]);
    }
    ans2 = ans1 = ans = 0;
    if (p == 1) {
      printf("%d\n", n % 2);
      continue;
    }
    v.clear();
    for (int i = 1; i <= n; ++i) {
      v.push_back(k[i]);
      was[k[i]]++;
    }
    while ((int)a.size()) a.pop();
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    reverse(v.begin(), v.end());
    for (int i = 0; i < (int)v.size(); ++i) {
      if (was[v[i]] % 2 == 0) {
        continue;
      } else {
        while ((int)a.size()) a.pop();
        long long last = ans;
        long long last1 = ans1;
        long long last2 = ans2;
        ans = add(ans, binpow(p, v[i], mod), mod);
        ans1 = add(ans1, binpow(p, v[i], mod1), mod1);
        ans2 = add(ans2, binpow(p, v[i], mod2), mod2);
        bool f = 0;
        int ni;
        for (int j = i + 1; j < (int)v.size(); ++j) {
          for (int k = 1; k <= was[v[j]]; ++k) {
            ans = add(ans, -binpow(p, v[j], mod), mod);
            ans1 = add(ans1, -binpow(p, v[j], mod1), mod1);
            ans2 = add(ans2, -binpow(p, v[j], mod2), mod2);
            if (last == ans && last1 == ans1 && last2 == ans2) {
              ni = j;
              f = 1;
              was[v[j]] -= k;
              break;
            }
          }
          if (f) break;
        }
        if (f) {
          i = ni - 1;
        } else {
          break;
        }
      }
    }
    for (int i = 1; i <= n; ++i) {
      was[k[i]] = 0;
    }
    printf("%lld\n", ans);
  }
}
