#include <bits/stdc++.h>   long long exp(long long x, long long p) {     long long ret = 1;     while (p > 0) {       if (p & 1) {         ret = (ret * x) % 1000000007;       }       p >>= 1;       x = (x * x) % 1000000007;     }     return ret;   }   signed main() {     std::ios::sync_with_stdio(false);     std::cin.tie(0);     std::cout.tie(0);     long long t;     std::cin >> t;     std::vector<long long> powers(2000000, 0);     while (t--) {       long long n, p;       std::cin >> n >> p;       long long arr[n];       for (long long i = 0; i < n; i++) {         std::cin >> arr[i];       }       std::sort(arr, arr + n);       if (p == 1) {         if (n % 2)           std::cout << 1 << std::endl;         else           std::cout << 0 << std::endl;         continue;       }       long long ans = 0;       std::set<long long> pows;       long long r = n - 1;       long long cur = -1;       while (r >= 0) {         cur = arr[r];         r--;         while (r >= 0) {           powers[arr[r]]++;           pows.insert(arr[r]);           long long next = arr[r];           while (powers[next] == p) {             powers[next] = 0;             pows.erase(next);             powers[next + 1]++;             pows.insert(next + 1);             next++;           }           if (powers[cur]) {             powers[cur] = 0;             cur = -1;             pows.erase(cur);             r--;             break;           }           r--;         }       }       while (!pows.empty()) {         long long u = *pows.begin();         pows.erase(pows.begin());         ans = (ans + (exp(p, u) * powers[u]) % 1000000007) % 1000000007;         powers[u] = 0;       }       if (cur != -1) {         ans = (exp(p, cur) + 1000000007 - ans) % 1000000007;       }       std::cout << ans << std::endl;     }     return 0;   }   