#pragma comment(linker, "/stack:20000000")
#define _CRT_SECURE_NO_WARNINGS

# include <iostream>
# include <cmath>
# include <algorithm>
# include <cstdio>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <deque>
# include <functional>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_map>
# else
# include <tr1/unordered_map>
using namespace std::tr1;
# endif
# else
# include <unordered_map>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,N,...) N

#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs

#define DBN1(a)         std::cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)       std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)     std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)   std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e) std::cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"

#define DEB(x) std::cerr<<#x<<"="<<(x)<<"  "

#ifdef DEBUG
#define DB(x) DEB(x)
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cout<<#a<<"=["; printarray(a,n); cout<<"]\n"
#define DAR(a,n,s) cout<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cout<<"]\n"
#else
#define DB(x) ((void)42)
#define DBN(...) ((void)42)
#define DA(a,n) ((void)42)
#define DAR(a,n,s) ((void)42)
#endif

double __begin;
#define DTIME(ccc) __begin = clock(); ccc; std::cerr<<"Time of work = "<<(clock()-__begin)/CLOCKS_PER_SEC<<std::endl;

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;

template<typename T1,typename T2,typename T3>
struct triple{T1 a;T2 b;T3 c;triple(){};triple(T1 _a,T2 _b, T3 _c):a(_a),b(_b),c(_c){}};
#define tri triple<int,int,int>
#define trl triple<ll,ll,ll>
template<typename T1,typename T2,typename T3>
bool operator<(const triple<T1,T2,T3> &t1,const triple<T1,T2,T3> &t2){if (t1.a!=t2.a) return t1.a<t2.a; else return t1.b<t2.b;}
template<typename T1, typename T2, typename T3>
inline std::ostream& operator << (std::ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }

#define FI(n) for(int i=0;i<n;i++)
#define FI1(n) for(int i=1;i<n;i++)
#define FJ(n) for(int j=0;j<n;j++)
#define FJ1(n) for(int j=1;j<n;j++)
#define FJI for(int j=0;j<i;j++)
#define FJI1 for(int j=1;j<i;j++)
#define FJIE for(int j=0;j<=i;j++)
#define FJIE1 for(int j=1;j<=i;j++)
#define For(i,a,b) for(int i=a;i<b;i++)

// Input macros
#define S(n)                        scanf("%d",&n)
#define S2(a,b)                     scanf("%d%d",&a,&b)
#define Sc(n)                       scanf("%c",&n)
#define Sl(n)                       scanf("%I64d",&n)
#define Sl2(a,b)                    scanf("%I64d%I63d",&a,&b)
#define Sf(n)                       scanf("%lf",&n)
#define Sf2(a,b)                    scanf("%lf%lf",&a,&b)

//Output macros
#define Pr(n)                       printf("%d ",n)
#define Prn(n)                      printf("%d\n",n)
#define Prc(n)                      printf("%c",n)
#define Prcn(n)                     printf("%c\n",n)
#define Prl(n)                      printf("%I64d ",n)
#define Prln(n)                     printf("%I64d\n",n)
#define Prf(n)                      printf("%lf ",n)
#define Prfn(n)                     printf("%lf\n",n)
#define Prs(n)                      printf("%s ",n)
#define Prsn(n)                     printf("%s\n",n)

//STL containers
#define foreach(v, c)               for( auto v = (c).begin();  v != (c).end(); ++v)
#define all(a)                      a.begin(), a.end()
#define in(a,b)                     ((b).find(a) != (b).end())
#define in2(a,b)                    (find(all(b),a) != (b).end())
#define pb                          push_back
#define sz(a)                       ((int)(a.size()))
#define svi(vec,n) FI(n) S(vec[y])
#define svl(vec,n) FI(n) Sl(vec[y])
#define IT ::iterator

//int some_primes[10] = {100271, 500179, 1000003, 2000227, 5000321}
//fill char arrays
#define fill(a,v)                    memset(a, v, sizeof (a))

inline int bits_count(int v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int bits_count(ll v){ int t = v >> 32; int p = (v & ((1LL << 32) - 1)); return bits_count(t) + bits_count(p); }
unsigned int reverse_bits(register unsigned int x){x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));return((x >> 16) | (x << 16));}
inline int sign(int x){return (x >> 31) | (-x >> 31);}
inline bool ispow2(int x){return (x!=0 && (x&(x-1))==0);}
#define checkbit(n,b)        ( (n >> b) & 1)
#define INDEX(arr,ind)       (lower_bound(all(arr),ind)-arr.begin())
#define sor(a)  sort(all(a))
#define rsor(a) sort(all(a)); reverse(all(a));

//STL output ********************************
template<typename T1,typename T2>inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p){return os << "(" << p.first << ", " << p.second << ")";}
template<typename T>inline std::ostream &operator<<(std::ostream &os,const std::vector<T>& v){bool first=true;os<<"[";for(unsigned int i=0;i<v.size();i++){if(!first)os<<", ";os<<v[i];first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator<<(std::ostream &os,const std::set<T1,T2>&v){bool first=true;os << "[";for(typename std::set<T1,T2>::const_iterator ii=v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T1, typename T2>inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v){bool first = true; os << "[";for (typename std::map<T1,T2>::const_iterator ii =v.begin();ii!=v.end();++ii){if(!first)os<<", ";os<<*ii;first=false;}return os<<"]";}
template<typename T,typename T2>void printarray(T  a[],T2 sz,T2 beg=0){for(T2 i=beg;i<sz;i++) cout<<a[i]<<" ";cout<<endl;}

#define FREIN(FILE) freopen(FILE,"rt",stdin)
#define FREOUT(FILE) freopen(FILE,"wt",stdout)

#define sqr(x) ((x)*(x))
#define sqrt(x) sqrt(1.0*(x))
#define pow(x,n) pow(1.0*(x),n)

inline ll binpow(ll x,ll n){ll res=1;while(n){if(n&1)res*=x;x*=x;n>>=1;}return res;}
inline ll powmod(ll x,ll n, ll _mod){ll res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
inline ll gcd(ll a,ll b){ll t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline int gcd(int a,int b){int t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
inline ll lcm(int a,int b){return a/gcd(a,b)*(ll)b;}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll gcd(ll a,ll b,ll c){return gcd(gcd(a,b),c);}
inline int gcd(int a,int b,int c){return gcd(gcd(a,b),c);}
inline ll lcm(ll a,ll b,ll c){return lcm(lcm(a,b),c);}
inline ll lcm(int a,int b,int c){return lcm(lcm(a,b),(ll)c);}

inline ll max(ll a,ll b){return (a>b)?a:b;}
inline int max(int a,int b){return (a>b)?a:b;}
inline double max(double a,double b){return (a>b)?a:b;}
inline ll max(ll a,ll b,ll c){return max(a,max(b,c));}
inline int max(int a,int b,int c){return max(a,max(b,c));}
inline double max(double a,double b,double c){return max(a,max(b,c));}
inline ll min(ll a,ll b){return (a<b)?a:b;}
inline int min(int a,int b){return (a<b)?a:b;}
inline double min(double a,double b){return (a<b)?a:b;}
inline ll min(ll a,ll b,ll c){return min(a,min(b,c));}
inline int min(int a,int b,int c){return min(a,min(b,c));}
inline double min(double a,double b,double c){return min(a,min(b,c));}

template<class T>
inline void getar(T a, int n, int m){ for (int i = 0; i < n; i++) for (int j = 0; j < m;++j) { scanf("%d", &a[i][j]); } }
inline void getar(int *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d", a + ii); } }
inline void getar(pii *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%d%d", &a[ii].first, &a[ii].second); } }
inline void getar(ll *a, int n){ for (int ii = 0; ii < n; ii++){ scanf("%I64d", a + ii); } }

// Useful constants

#define INF         1011111111
#define EPS         (double)1e-8
#define mod         1000000007
#define PI          3.14159265358979
//*******************************************************************************/

int pr[100010];
vector<int> primes;
vector<int> d[100001];
vector<int> v[100001];
int t[100001];
bool used[100001];
int z[100001];
struct Comp
{
    bool operator()(const int&x, const int&y)
    {
        return t[x] < t[y];
    }
};
set<int, Comp> s;
vector<pii> ans;
bool cmp(const int &x, const int& y)
{
    return z[x] > z[y];
}
int main()
{
    int n;
    cin >> n;
    for (int i = 2; i <= n; ++i)
    {
        if (pr[i] == 0)
        {
            pr[i] = i;
            primes.push_back(i);
        }
        for (int x : primes)
        {
            if (x * i > n) break;
            if (pr[i] < x) break;
            pr[x * i] = x;
        }
    }
    for (int i = n; i >= 2; --i)
    {
        int x = i;
        while (x != 1){
            int p = pr[x];
            d[i].push_back(p);
            while (pr[x] == p)
            {
                x /= p;
            }
            z[i] -= n / p;
            v[p].push_back(i);
        }
    }
    for (int i = 2; i <= n; ++i)
    {
        if (v[i].size() > 1)
        {
            sort(all(v[i]), cmp);
            //DBN(i, v[i]);
            t[i] = v[i].size();
            s.insert(i);
        }
    }
    while (!s.empty())
    {
        int p = *s.begin();
        s.erase(s.begin());

        int x = 0;
        int y = 0;
        for (int i = v[p].size() - 1; i >= 0; --i)
        {
            if (y) break;
            int val = v[p][i];
            if (!used[val])
            {
                if (!x) x = val;
                else y = val;
            }
            v[p].pop_back();
        }
        ans.push_back({ x, y });
        for (int p : d[x])
        {
            t[p]--;
        }
        for (int p : d[y])
        {
            t[p]--;
        }
        if (t[p] > 1)
            s.insert(p);
    }
    cout << ans.size() << '\n';
    for (pii p : ans)
    {
        cout << p.first << ' ' << p.second << '\n';
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int mod1 = 1000000007;
const int mod2 = 998244353;
#define int long long
int T, n, k, a[100007], s1, s2;
int pow(int a, int b, int mod)
{
	int s = 1, x = a;
	while(b > 0)
    {
		if(b & 1) s = (s * x) % mod;
        x = (x * x) % mod;
		b >>= 1;
	}
	return s;
}
signed main()
{
	cin >> T;
	while (T--)
	{
		s1 = s2 = 0;
		scanf ("%lld%lld", &n, &k);
		for (register int i = 1; i <= n; i++)
			cin >> a[i];
		if (k == 1)
		{
			s1 = n % 2;
			cout << s1 << endl;
			continue;
		}
		sort (a + 1, a + n + 1, greater<int>());
		for (register int i = 1; i <= n; i++)
		{
			int t1 = pow (k, a[i], mod1), t2 = pow (k, a[i], mod2);
			if (!s1 && !s2)
			{
				s1 = t1;
				s2 = t2;
			}
			else
			{
				s1 = (s1 - t1 + mod1) % mod1;
				s2 = (s2 - t2 + mod2) % mod2;
			}
		}
		cout << s1 << endl;
	}
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

const int MAXN = 1000005;
const ll MOD = 1000000007;

int T,n,p;
int a[MAXN];

ll power(ll a,ll b)
{
	ll res = 1;
	while (b)
	{
		if (b & 1)
			(res *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&p);
		for (int i = 1;i <= n;i++)
			scanf("%d",&a[i]);
		sort(a + 1,a + n + 1);
		if (p == 1)
		{
			printf("%d\n",(n & 1) ? 1 : 0);
			continue;
		}
		ll dif = 0,dif2 = 0;
		for (int i = n;i >= 1;i--)
		{
			if (i < n && a[i] != a[i + 1])
			{
				ll g = power(p,a[i + 1] - a[i]);
				(dif2 *= g) %= MOD;
				for (int j = 1;j <= a[i + 1] - a[i];j++)
				{
					dif *= p;
					if (dif >= n)
					{
						dif = n;
						break;
					}
				}
			}
			if (!dif)
				dif = dif2 = 1;
			else
			{
				dif--;
				dif2--;
			}
		}
		(dif2 *= power(p,a[1])) %= MOD;
		printf("%lld\n",dif2);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

const int MAXN = 1000005;
const ll MOD = 1000000007;

int T,n,p;
int a[MAXN];

ll power(ll a,ll b)
{
	ll res = 1;
	while (b)
	{
		if (b & 1)
			(res *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&p);
		for (int i = 1;i <= n;i++)
			scanf("%d",&a[i]);
		sort(a + 1,a + n + 1);
		if (p == 1)
		{
			printf("%d\n",(n & 1) ? 1 : 0);
			continue;
		}
		ll dif = 0,dif2 = 0;
		for (int i = n;i >= 1;i--)
		{
			if (i < n && a[i] != a[i + 1])
			{
				ll g = power(p,a[i + 1] - a[i]);
				(dif2 *= g) %= MOD;
				for (int j = 1;j <= a[i + 1] - a[i] && dif;j++)
				{
					dif *= p;
					if (dif >= n)
					{
						dif = n;
						break;
					}
				}
			}
			if (!dif)
				dif = dif2 = 1;
			else
			{
				dif--;
				dif2--;
			}
		}
		(dif2 *= power(p,a[1])) %= MOD;
		printf("%lld\n",(dif2 + MOD) % MOD);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

const int MAXN = 1000005;
const ll MOD = 1000000007;

int T,n,p;
int a[MAXN];

ll power(ll a,ll b)
{
	ll res = 1;
	while (b)
	{
		if (b & 1)
			(res *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&p);
		for (int i = 1;i <= n;i++)
			//scanf("%d",&a[i]);
			a[i] = rand() % 1000000 + 1;
		sort(a + 1,a + n + 1);
		if (p == 1)
		{
			printf("%d\n",(n & 1) ? 1 : 0);
			continue;
		}
		ll dif = 0,dif2 = 0;
		for (int i = n;i >= 1;i--)
		{
			if (i < n && a[i] != a[i + 1])
			{
				ll g = power(p,a[i + 1] - a[i]);
				(dif2 *= g) %= MOD;
				for (int j = 1;j <= a[i + 1] - a[i];j++)
				{
					dif *= p;
					if (dif >= n)
					{
						dif = n;
						break;
					}
				}
			}
			if (!dif)
				dif = dif2 = 1;
			else
			{
				dif--;
				dif2--;
			}
		}
		(dif2 *= power(p,a[1])) %= MOD;
		printf("%lld\n",(dif2 + MOD) % MOD);
	}
	return 0;
}


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

typedef long long ll;

const int MAXN = 1000005;
const ll MOD = 1000000007;

int T,n,p;
int a[MAXN];

ll power(ll a,ll b)
{
	ll res = 1;
	while (b)
	{
		if (b & 1)
			(res *= a) %= MOD;
		(a *= a) %= MOD;
		b >>= 1;
	}
	return res;
}

int main()
{
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&p);
		for (int i = 1;i <= n;i++)
			scanf("%d",&a[i]);
		sort(a + 1,a + n + 1);
		if (p == 1)
		{
			printf("%d\n",(n & 1) ? 1 : 0);
			continue;
		}
		ll dif = 0,dif2 = 0;
		for (int i = n;i >= 1;i--)
		{
			if (i < n && a[i] != a[i + 1])
			{
				ll g = power(p,a[i + 1] - a[i]);
				(dif2 *= g) %= MOD;
				for (int j = 1;j <= a[i + 1] - a[i];j++)
				{
					dif *= p;
					if (dif >= n)
					{
						dif = n;
						break;
					}
				}
			}
			if (!dif)
				dif = dif2 = 1;
			else
			{
				dif--;
				dif2--;
			}
		}
		(dif2 *= power(p,a[1])) %= MOD;
		printf("%lld\n",(dif2 + MOD) % MOD);
	}
	return 0;
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}
		sort(a + 1, a + n + 1);

		int pos = n;
		ll v, lv;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				if (--pos == 0) goto bakapianotxdy;
				while (lv > a[pos]) {
					if (v * p > pos) goto bakapianotxdy;
					v *= p;
					lv--;
				}
				v--;
			}
			if (--pos == 0) break;
		}
		bakapianotxdy :
		//cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans = ans - pre[pos] + mod;
		cout << ans % mod << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}
		sort(a + 1, a + n + 1);

		int pos = n;
		ll v, lv;
		int succ;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				pos--;
				succ = 1;
				while (lv > a[pos]) {
					if (v * p > pos) {
						succ = 0;
						break;
					} else {
						v *= p;
						lv--;
					}
				}
				if (!succ) break;
				else v--;
			}

			if (!succ) break;
			if (--pos == 0) break;
		}
		cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans = ans - pre[pos] + mod;
		cout << ans % mod << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll getinv (ll a) {
	return qpow(a, mod - 2);
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}
		sort(a + 1, a + n + 1);

		int pos = n;
		ll v, lv;
		int succ;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				pos--;
				succ = 1;
				while (lv > a[pos]) {
					if (v * p > pos) {
						succ = 0;
						break;
					} else {
						v *= p;
						lv--;
					}
				}

				if (!succ) break;
				else v--;
			}

			if (!succ) break;
			if (--pos == 0) break;
		}

		ll ans = v * qpow(p, lv) % mod;
		ans = ans - pre[pos] + mod;
		cout << ans % mod << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		for (int i = 1;i <= n;++i) {
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}

		if (p == 1) {
			cout << (n & 1) << endl;
			continue;
		}

		int pos = n;
		ll v, lv;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				if (--pos == 0) goto bakapianotxdy;
				while (lv > a[pos]) {
					if (v * p > pos) goto bakapianotxdy;
					v *= p;
					lv--;
				}
				v--;
			}
			if (--pos == 0) break;
		}

		bakapianotxdy :
		//cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans -= pre[pos];
		ans %= mod;
		ans += mod;
		ans %= mod;
		cout << ans << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}
		sort(a + 1, a + n + 1);

		int pos = n;
		ll v, lv;
		int succ;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				pos--;
				succ = 1;
				while (lv > a[pos]) {
					if (v * p > pos) {
						succ = 0;
						break;
					} else {
						v *= p;
						lv--;
					}
				}
				if (!succ) break;
				else v--;
			}

			if (!succ) break;
			if (--pos == 0) break;
		}
		//cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans = ans - pre[pos] + mod;
		cout << ans % mod << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}
		sort(a + 1, a + n + 1);

		if (p == 1) {
			cout << (n & 1) << endl;
			continue;
		}

		int pos = n;
		ll v, lv;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				if (--pos == 0) goto bakapianotxdy;
				while (lv > a[pos]) {
					if (v * p > pos) goto bakapianotxdy;
					v *= p;
					lv--;
				}
				v--;
			}
			if (--pos == 0) break;
		}

		bakapianotxdy :
		//cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans -= pre[pos];
		ans %= mod;
		ans += mod;
		ans %= mod;
		cout << ans << endl;
	}
}


#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdio>
#define all(v) (v).begin(), (v).end()
#define unq(v) (v).erase(unique(all(v)), (v).end())
#define ii int, int
#define li int, int
#define ll2 ll, ll
#define vec vector
#define pii pair <int, int>
#define pli pair <ll, int>
#define pll2 pair <ll, ll>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define MULTI int T; cin >> T; while(T--)
#define sqr(x) ((x) * (x))
#define test cerr << '!' << endl;
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main0 ();
int main () {
	#ifndef ONLINE_JUDGE
		freopen("C:\\Users\\98497\\Desktop\\code\\file.in", "r", stdin);
	#endif
	ios::sync_with_stdio(false);
	clock_t start, end;
	// start = clock();
	main0();
	// end = clock();
	// cout << (end - start) << endl;
	#ifndef ONLINE_JUDGE
		fclose(stdin);
	#endif
	return 0;
}

const int dx[8] = {0, 1, -1, 0, 1, 1, -1, -1};
const int dy[8] = {1, 0, 0, -1, 1, -1, -1, 1};
const int N = 1e6 + 5;
const int M = 1e5;
const int INF = 0x3f3f3f3f;
const ll INFF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;

ll qpow (ll n, ll m) {
	ll ret = 1;
	while (m) {
		if (m & 1) ret = ret * n % mod;
		n = n * n % mod;
		m >>= 1;
	}
	return ret;
}

ll a[N], pre[N];

int main0 () {
	MULTI {
		ll n, p;
		cin >> n >> p;
		for (int i = 1;i <= n;++i) {
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);
		for (int i = 1;i <= n;++i) {
			pre[i] = pre[i - 1] + qpow(p, a[i]);
			if (pre[i] >= mod) pre[i] -= mod;
		}

		if (p == 1) {
			cout << (n & 1) << endl;
			continue;
		}

		int pos = n;
		ll v, lv;
		while (true) {
			v = 1, lv = a[pos];
			while (v) {
				if (--pos == 0) goto bakapianotxdy;
				while (lv > a[pos]) {
					if (v * p > pos) goto bakapianotxdy;
					v *= p;
					lv--;
				}
				v--;
			}
			if (--pos == 0) break;
		}

		bakapianotxdy :
		cout << v << ' ' << lv << ' ' << pos << endl;
		ll ans = v % mod * qpow(p, lv) % mod;
		ans -= pre[pos];
		ans %= mod;
		ans += mod;
		ans %= mod;
		cout << ans << endl;
	}
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define rsz resize
#define ins insert
#define mp make_pair
#define beg(x) x.begin()
#define en(x) x.end()
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue

#define ffor(i, a, b) for (int (i)=a; i<b; ++i)
#define rfor(i, a, b) for (int (i)=b-1; i>=a; --i)
#define ff0r(i, a) for (int (i)=0; i<a; ++i)
#define rf0r(i, a) for (int (i)=a-1; i>=0; --i)
#define in(i, x) for (auto& i : x)
#define fitfor(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
#define ritfor(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)

using ll=long long;
using db=double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;
using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> bool ckmin(T& a, const T& b) {return (b<a)?a=b, 1:0;}
template<class T> bool ckmax(T& a, const T& b) {return (a<b)?a=b, 1:0;}

const ll MOD=1e9+7;
const ll MOD2=998244353;
const int kx[]={0,1,0,-1}, ky[]={1,0,-1,0};

void fileIO(string filename) {
    freopen((filename+".in").c_str(), "r", stdin);
    freopen((filename+".out").c_str(), "w", stdout);
}

ll binpow(ll a, ll b) {
    ll ans=1;
    while (b>0) {
        if (b&1)
            ans=ans*a%MOD;
        a=a*a%MOD;
        b>>=1;
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //fileIO("");
    int t;
    cin >> t;
    while (t--) {
        ll p, n;
        cin >> n >> p;
        vl a(n);
        ff0r(i, n) cin >> a[i];
        sort(all(a), greater<ll>());
        ll ans=0;
        ff0r(i, n) {
            if (ans)
                ans=(ans-binpow(p, a[i])+MOD)%MOD;
            else
                ans=(ans+binpow(p, a[i])+MOD)%MOD;
        }
        cout << ans << '\n';
    }
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define rsz resize
#define ins insert
#define mp make_pair
#define beg(x) x.begin()
#define en(x) x.end()
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue

#define ffor(i, a, b) for (int (i)=a; i<b; ++i)
#define rfor(i, a, b) for (int (i)=b-1; i>=a; --i)
#define ff0r(i, a) for (int (i)=0; i<a; ++i)
#define rf0r(i, a) for (int (i)=a-1; i>=0; --i)
#define in(i, x) for (auto& i : x)
#define fitfor(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
#define ritfor(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)

using ll=long long;
using db=double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;
using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> bool ckmin(T& a, const T& b) {return (b<a)?a=b, 1:0;}
template<class T> bool ckmax(T& a, const T& b) {return (a<b)?a=b, 1:0;}

const ll MOD=1e9+7;
const ll MOD2=999995713;
const int kx[]={0,1,0,-1}, ky[]={1,0,-1,0};

void fileIO(string filename) {
    freopen((filename+".in").c_str(), "r", stdin);
    freopen((filename+".out").c_str(), "w", stdout);
}

ll binpow(ll a, ll b, ll mod) {
    ll ans=1;
    while (b>0) {
        if (b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //fileIO("");
    int t;
    cin >> t;
    while (t--) {
        ll p, n;
        cin >> n >> p;
        vl a(n);
        ff0r(i, n) cin >> a[i];
        sort(all(a), greater<ll>());
        ll ans=0, buf=0;
        ff0r(i, n) {
            if (!ans && !buf) {
                ans+=binpow(p, a[i], MOD);
                buf+=binpow(p, a[i], MOD2);
            }
            else {
                ans=(ans-binpow(p, a[i], MOD)+MOD)%MOD;
                buf=(buf-binpow(p, a[i], MOD2)+MOD2)%MOD2;
            }
        }
        cout << ans << '\n';
    }
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define rsz resize
#define ins insert
#define mp make_pair
#define beg(x) x.begin()
#define en(x) x.end()
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue

#define ffor(i, a, b) for (int (i)=a; i<b; ++i)
#define rfor(i, a, b) for (int (i)=b-1; i>=a; --i)
#define ff0r(i, a) for (int (i)=0; i<a; ++i)
#define rf0r(i, a) for (int (i)=a-1; i>=0; --i)
#define in(i, x) for (auto& i : x)
#define fitfor(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
#define ritfor(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)

using ll=long long;
using db=double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;
using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> bool ckmin(T& a, const T& b) {return (b<a)?a=b, 1:0;}
template<class T> bool ckmax(T& a, const T& b) {return (a<b)?a=b, 1:0;}

const ll MOD=1e9+7;
const ll MOD2=998244353;
const int kx[]={0,1,0,-1}, ky[]={1,0,-1,0};

void fileIO(string filename) {
    freopen((filename+".in").c_str(), "r", stdin);
    freopen((filename+".out").c_str(), "w", stdout);
}

ll binpow(ll a, ll b, ll mod) {
    ll ans=1;
    while (b>0) {
        if (b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //fileIO("");
    int t;
    cin >> t;
    while (t--) {
        ll p, n;
        cin >> n >> p;
        vl a(n);
        ff0r(i, n) cin >> a[i];
        sort(all(a), greater<ll>());
        ll ans=0, buf=0;
        ff0r(i, n) {
            if (!ans && !buf) {
                ans+=binpow(p, a[i], MOD);
                buf+=binpow(p, a[i], MOD2);
            }
            else {
                ans=(ans-binpow(p, a[i], MOD)+MOD)%MOD;
                buf=(buf-binpow(p, a[i], MOD2)+MOD2)%MOD2;
            }
        }
        cout << ans << '\n';
    }
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define rsz resize
#define ins insert
#define mp make_pair
#define beg(x) x.begin()
#define en(x) x.end()
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue

#define ffor(i, a, b) for (int (i)=a; i<b; ++i)
#define rfor(i, a, b) for (int (i)=b-1; i>=a; --i)
#define ff0r(i, a) for (int (i)=0; i<a; ++i)
#define rf0r(i, a) for (int (i)=a-1; i>=0; --i)
#define in(i, x) for (auto& i : x)
#define fitfor(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
#define ritfor(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)

using ll=long long;
using db=double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;
using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> bool ckmin(T& a, const T& b) {return (b<a)?a=b, 1:0;}
template<class T> bool ckmax(T& a, const T& b) {return (a<b)?a=b, 1:0;}

const ll MOD=1e9+7;
const ll MOD2=998244353;
const int kx[]={0,1,0,-1}, ky[]={1,0,-1,0};

void fileIO(string filename) {
    freopen((filename+".in").c_str(), "r", stdin);
    freopen((filename+".out").c_str(), "w", stdout);
}

ll binpow(ll a, ll b, ll mod) {
    ll ans=1;
    while (b>0) {
        if (b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //fileIO("");
    int t;
    cin >> t;
    while (t--) {
        ll p, n;
        cin >> n >> p;
        vl a(n);
        ff0r(i, n) cin >> a[i];
        sort(all(a), greater<ll>());
        ll ans=0, buf=0;
        ff0r(i, n) {
            if (ans && buf) {
                ans=(ans-binpow(p, a[i], MOD)+MOD)%MOD;
                buf=(buf-binpow(p, a[i], MOD2)+MOD2)%MOD2;
            }
            else {
                ans=(ans+binpow(p, a[i], MOD))%MOD;
                buf=(buf+binpow(p, a[i], MOD2))%MOD2;
            }
        }
        cout << ans << '\n';
    }
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define pb push_back
#define rsz resize
#define ins insert
#define mp make_pair
#define beg(x) x.begin()
#define en(x) x.end()
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define lb lower_bound
#define ub upper_bound
#define umap unordered_map
#define uset unordered_set
#define pqueue priority_queue

#define ffor(i, a, b) for (int (i)=a; i<b; ++i)
#define rfor(i, a, b) for (int (i)=b-1; i>=a; --i)
#define ff0r(i, a) for (int (i)=0; i<a; ++i)
#define rf0r(i, a) for (int (i)=a-1; i>=0; --i)
#define in(i, x) for (auto& i : x)
#define fitfor(i, x) for (auto i=(x).begin(); i!=(x).end(); ++i)
#define ritfor(i, x) for (auto i=(x).rbegin(); i!=(x).rend(); ++i)

using ll=long long;
using db=double;
using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;
using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> bool ckmin(T& a, const T& b) {return (b<a)?a=b, 1:0;}
template<class T> bool ckmax(T& a, const T& b) {return (a<b)?a=b, 1:0;}

const ll MOD=1e9+7;
const ll MOD2=998244353;
const int kx[]={0,1,0,-1}, ky[]={1,0,-1,0};

void fileIO(string filename) {
    freopen((filename+".in").c_str(), "r", stdin);
    freopen((filename+".out").c_str(), "w", stdout);
}

ll binpow(ll a, ll b, ll mod) {
    ll ans=1;
    while (b>0) {
        if (b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}

int main(void) {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //fileIO("");
    int t;
    cin >> t;
    while (t--) {
        ll p, n;
        cin >> n >> p;
        vl a(n);
        ff0r(i, n) cin >> a[i];
        sort(all(a), greater<ll>());
        ll ans=0, buf=0;
        ff0r(i, n) {
            if (!ans && !buf) {
                ans=(ans+binpow(p, a[i], MOD))%MOD;
                buf=(buf+binpow(p, a[i], MOD2))%MOD2;
            }
            else {
                ans=(ans-binpow(p, a[i], MOD)+MOD)%MOD;
                buf=(buf-binpow(p, a[i], MOD2)+MOD2)%MOD2;
            }
        }
        cout << ans << '\n';
    }
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=1;
	while(a){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					map<int,int> m;
					for(int j=i-1;j>=0;j--){
						m[a[j]]++;
						int k=a[j];
						while(m[k]==p){
							m[k+1]++;
							m[k]=0;
							k++;
							if(k==a[i]){
								i=j;
								a[j]=0;
								break;
							}
						}
					}
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt>=p&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
                    int flag=0;
                    map<int,int> m;
                    for(int j=i-1;j>=0&&!flag;j--){
                        m[a[j]]++;
                        int k=a[j];
                        while(m[k]==p){
                            m[k+1]++;
                            m[k]=0;
                            k++;
                            if(k==a[i]){
                                i=j;
                                a[j]=0;
                                flag=1;
                                break;
                            }
                        }
                    }
                    assert(flag);
                }
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt>=p&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					map<int,int> m;
					for(int j=i-1;j>=0;j--){
						m[a[j]]++;
						int k=a[j];
						while(m[k]==p){
							m[k+1]++;
							m[k]=0;
							k++;
							if(k==a[i]){
								i=j;
								a[j]=0;
								break;
							}
						}
					}
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

bool check(int x,int v,int d){
	int now=a[x],cnt=0;
	for(int i=x;i<d;i++){
		while(cnt&&now<a[i]){
			now++;
			cnt/=p;
		}
		if(now<a[i]) now=a[i],cnt=1;
		else cnt++;
	}
	while(cnt){
		now++;
		cnt/=p;
	}
	return now>=v;
}

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		// for(int i=0;i<n;i++) printf("%d ",pre[i]);
			// puts("");
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					int l=0,r=i-1;
					while(r-l>1){
						int mid=(l+r)>>1;
						if(check(mid,a[i],i)){
							l=mid;
						}
						else r=mid;
					}
					i=l;
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		// for(int i=0;i<n;i++) printf("%d ",pre[i]);
			// puts("");
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					map<int,int> m;
					for(int j=i-1;j>=0;j--){
						m[a[j]]++;
						int k=a[j];
						while(m[k]==p){
							m[k+1]++;
							m[k]=0;
							k++;
							if(k==a[i]){
								i=j;
								a[j]=-1;
								break;
							}
						}
					}
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt>=p&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
                    int flag=0;
                    map<int,int> m;
                    for(int j=i-1;j>=0;j--){
                        m[a[j]]++;
                        int k=a[j];
                        while(m[k]==p){
                            m[k+1]++;
                            m[k]=0;
                            k++;
                            if(k==a[i]){
                                i=j;
                                a[j]=0;
                                flag=1;
                                break;
                            }
                        }
                    }
                    assert(flag);
                }
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int _log(int a,int b){
	int res=0;
	while(a>=b){
		a/=b;
		res++;
	}
	return res;
}
int n,p;
int a[maxn];

int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);
		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
			else{
				while(cnt&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+_log(cnt,p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					map<int,int> m;
					for(int j=i-1;j>=0;j--){
						m[a[j]]++;
						int k=a[j];
						while(m[k]==p){
							m[k+1]++;
							m[k]=0;
							k++;
							if(k==a[i]){
								i=j;
								a[j]=0;
								break;
							}
						}
					}
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>
using namespace std;
const int maxn=1e6+23;
typedef long long ll;

const int mod=1e9+7;
int pre[maxn];
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1) ans=1ll*a*ans%mod;
		b>>=1;
		a=1ll*a*a%mod;
	}
	return ans;
}
int n,p;
int a[maxn];
int main(){
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d",&n,&p);
		for(int i=0;i<n;i++) scanf("%d",&a[i]);

		if(p==1) {printf("%d\n",n%2);continue;}
		sort(a,a+n);
		int now=a[0],cnt=0;
		for(int i=0;i<n;i++){
			if(a[i]==now){
				cnt++;
				pre[i]=a[i]+(cnt/p);
			}
			else{
				while(cnt&&now<a[i]){
					now++;
					cnt/=p;
				}
				if(now<a[i]) now=a[i],cnt=1;
				else cnt++;
				pre[i]=a[i]+(cnt/p);
			}
		}
		ll ans=0;
		cnt=1;
		for(int i=n-1;i>=0;i--){
			if(i>0&&a[i]==a[i-1]){
				cnt++;
			}
			else{
				// printf("%d %d\n",i,cnt);
				if(cnt%2==0) cnt=0;
				else if(i>0&&pre[i-1]>=a[i]){
					map<int,int> m;
					for(int j=i-1;j>=0;j--){
						m[a[j]]++;
						int k=a[j];
						while(m[k]==p){
							m[k+1]++;
							m[k]=0;
							k++;
							if(k==a[i]){
								i=j;
								a[j]=0;
								break;
							}
						}
					}
				}
				else{
					ll t=0;
					for(int k=0;k<i;k++){
						t+=qpow(p,a[k]);
						t%=mod;
					}
					ans=(qpow(p,a[i])+mod-t)%mod;
					break;
				}
				cnt=1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<ll> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
	a %= MOD;
	b%= MOD;
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll tstst;
	cin>>tstst;
	forn(tst,tstst){
		ll n,p;
		cin>>n>>p;
		multiset<ll> set1;
		set1.clear();
		forn(i,n){
			ll x;
			cin>>x;
			set1.insert(x);
		}
		if(p==1){
			if(n%2==0){
				cout<<0<<'\n';
			}else{
				cout<<1<<'\n';
			}
			continue;
		}
		ll diff=0;
		ll curk=-5;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){

		//	cout<<diff<<'\n';
			it--;
			if(curk==-5){
				curk=*it;
			}
			if((*it)!=curk){

				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if((*it)!=curk){
				break;
			}
			if(fail>0){
				break;
			}

			diff-=1;
				if(diff<0){
				diff=-diff;
			}

		//	cout<<diff<<'\n';
		}
		if(fail>0){
			it++;

		 diff=modmult(diff, modpow(p,curk));

		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
			diff+=MOD;
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//prllf("Case #%d ",tst+1);
	}
}

//priority_queue<ll,vector<ll>,greater<ll>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (int i = (int)(a); i < (int)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int tstst;
	cin>>tstst;
	forn(tst,tstst){
		int n,p;
		cin>>n>>p;
		multiset<int> set1;
		set1.clear();
		forn(i,n){
			int x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-1;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){
		//	cout<<diff<<'\n';
			it--;
			if(curk==-1){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{
					ll cand=modmult(diff,modpow(p,curk-(*it)));
					if(cand>1e6){
						fail++;
						break;
					}
					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if(fail>0){
				break;
			}
			diff-=1;

		}
		if(fail>0){
			it++;
		if(diff<0){
				diff=-diff;
	}
		 diff=modmult(diff, modpow(p,curk));
		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//printf("Case #%d ",tst+1);
	}
}

//priority_queue<int,vector<int>,greater<int>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<ll> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
	a %= MOD;
	b%= MOD;
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll tstst;
	cin>>tstst;
	forn(tst,tstst){
		ll n,p;
		cin>>n>>p;
		multiset<ll> set1;
		set1.clear();
		forn(i,n){
			ll x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-5;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){

		//	cout<<diff<<'\n';
			it--;
			if(curk==-5){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if((*it)!=curk){
				break;
			}
			if(fail>0){
				break;
			}

			diff-=1;
				if(diff<0){
				diff=-diff;
			}

		//	cout<<diff<<'\n';
		}
		if(fail>0){
			it++;

		 diff=modmult(diff, modpow(p,curk));

		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
			diff+=MOD;
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//prllf("Case #%d ",tst+1);
	}
}

//priority_queue<ll,vector<ll>,greater<ll>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<ll> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll tstst;
	cin>>tstst;
	forn(tst,tstst){
		ll n,p;
		cin>>n>>p;
		multiset<ll> set1;
		set1.clear();
		forn(i,n){
			ll x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-5;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){

		//	cout<<diff<<'\n';
			it--;
			if(curk==-5){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if((*it)!=curk){
				break;
			}

			diff-=1;
				if(diff<0){
				diff=-diff;
			}

		}
		if(fail>0){
			it++;

		 diff=modmult(diff, modpow(p,curk));

		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
			diff+=MOD;
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//prllf("Case #%d ",tst+1);
	}
}

//priority_queue<ll,vector<ll>,greater<ll>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (int i = (int)(a); i < (int)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	int tstst;
	cin>>tstst;
	forn(tst,tstst){
		int n,p;
		cin>>n>>p;
		multiset<int> set1;
		set1.clear();
		forn(i,n){
			int x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-1;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){
		//	cout<<diff<<'\n';
			it--;
			if(curk==-1){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{
					ll cand=modmult(diff,modpow(p,curk-(*it)));

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if(fail>0){
				break;
			}
			diff-=1;

		}
		if(fail>0){
			it++;
		if(diff<0){
				diff=-diff;
	}
		 diff=modmult(diff, modpow(p,curk));
		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//printf("Case #%d ",tst+1);
	}
}

//priority_queue<int,vector<int>,greater<int>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (int i = (int)(a); i < (int)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	int tstst;
	cin>>tstst;
	forn(tst,tstst){
		int n,p;
		cin>>n>>p;
		multiset<int> set1;
		set1.clear();
		forn(i,n){
			int x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-1;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){
		//	cout<<diff<<'\n';
			it--;
			if(curk==-1){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{
					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if(fail>0){
				break;
			}
			diff-=1;

		}
		if(fail>0){
			it++;
		if(diff<0){
				diff=-diff;
	}
		 diff=modmult(diff, modpow(p,curk));
		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
			diff+=MOD;
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}

		cout<<diff<<'\n';

		//printf("Case #%d ",tst+1);
	}
}

//priority_queue<int,vector<int>,greater<int>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<ll> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll tstst;
	cin>>tstst;
	forn(tst,tstst){
		ll n,p;
		cin>>n>>p;
		multiset<ll> set1;
		set1.clear();
		forn(i,n){
			ll x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-5;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){

		//	cout<<diff<<'\n';
			it--;
			if(curk==-5){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if((*it)!=curk){
				break;
			}
			if(fail>0){
				break;
			}

			diff-=1;
				if(diff<0){
				diff=-diff;
			}

		//	cout<<diff<<'\n';
		}
		if(fail>0){
			it++;

		 diff=modmult(diff, modpow(p,curk));

		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
			diff+=MOD;
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//prllf("Case #%d ",tst+1);
	}
}

//priority_queue<ll,vector<ll>,greater<ll>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (ll i = 0; i < (ll)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<ll> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	ll tstst;
	cin>>tstst;
	forn(tst,tstst){
		ll n,p;
		cin>>n>>p;
		multiset<ll> set1;
		set1.clear();
		forn(i,n){
			ll x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-1;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){
		//	cout<<diff<<'\n';
			it--;
			if(curk==-1){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if(fail>0){
				break;
			}
			diff-=1;

		}
		if(fail>0){
			it++;
		if(diff<0){
				diff=-diff;
	}
		 diff=modmult(diff, modpow(p,curk));
		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//prllf("Case #%d ",tst+1);
	}
}

//priority_queue<ll,vector<ll>,greater<ll>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include <bits/stdc++.h>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define fornll(i, n) for (ll i = 0; i < (ll)(n); i++)
#define forex(i, a, b) for (int i = (int)(a); i < (int)(b); i++ )
#define forexll(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++ )
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
typedef string st;
typedef vector<string> vst;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char dc[4] = {'N','S','E','W'};
//1e9+7, 2e5+5
#define MOD 1000000007
#define SIZE 200005

ll modmult(ll a, ll b) {
    return (long long)a * b % MOD;
}

// inverse of a modulo MOD
/*
ll inverse(ll a) {
	if(inversestore[a]!=0){
		return inversestore[a];
	}

    ll x, y;
    xGCD(a, MOD, x, y);
    inversestore[a]=(x+MOD)%MOD;
    return inversestore[a];
}*/

ll modpow(ll base, ll exp)
{
	ll result = 1;
	for (base %= MOD; exp; exp >>= 1) {
	if (exp & 1) result = (result * base) % MOD;
	base = (base * base) % MOD;
	}
	return result;
}

int main(){
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int tstst;
	cin>>tstst;
	forn(tst,tstst){
		int n,p;
		cin>>n>>p;
		multiset<int> set1;
		set1.clear();
		forn(i,n){
			int x;
			cin>>x;
			set1.insert(x);
		}
		ll diff=0;
		ll curk=-1;
		ll fail=0;
		auto it=set1.end();
		while(it!=set1.begin()){
		//	cout<<diff<<'\n';
			it--;
			if(curk==-1){
				curk=*it;
			}
			if((*it)!=curk){
				if(diff==0){
					curk=*it;
				}else{

					while((*it)!=curk){
					curk--;
					diff*=p;
					if(abs(diff)>1e6){
							fail++;
							break;
						}
					}
				}

			}
			if(diff<0){
				diff=-diff;
			}

			if(fail>0){
				break;
			}
			diff-=1;

		}
		if(fail>0){
			it++;
		if(diff<0){
				diff=-diff;
	}
		 diff=modmult(diff, modpow(p,curk));
		while(it!=set1.begin()){
			it--;
			diff-=modpow(p,(*it));
			diff%=MOD;
		}

		}
		if(fail==0){
			if(diff<0){
				diff=-diff;
			}
			diff=modmult(diff, modpow(p,curk));
		}
		diff%=MOD;
		diff+=MOD;
		diff%=MOD;

		cout<<diff<<'\n';

		//printf("Case #%d ",tst+1);
	}
}

//priority_queue<int,vector<int>,greater<int>> q; (remove smallest ele)
//freopen("teleport.in","r",stdin);
//freopen("teleport.out","w",stdout);


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
const int maxn=1e6+5;
const int lzy1=1e9+7;
const int lzy2=998244353;
const int lzy3=114514191;
int yzakioi;
int n,k;
int shu[maxn];
int ans1,ans2,ans3;
inline bool cmp(int x,int y){return x>y;}
inline int qm(int x,int y,int lzy)
{
	int jie=1;
	for(;y;y>>=1,x=(x*x)%lzy)
	if(y&1)
	jie*=x,jie%=lzy;
	return jie;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin>>yzakioi;
	while(yzakioi--)
	{
		cin>>n>>k;
		for(int i=1;i<=n;i++)
		cin>>shu[i];
		if(k==1)
		{
			cout<<n%2<<"\n";
			continue;
		}
		sort(shu+1,shu+1+n,cmp);
		ans1=ans2=ans3=0;
		for(int i=1;i<=n;i++)
		if(!ans1&&!ans2&&!ans3)
		{
	    	ans1=qm(k,shu[i],lzy1);
	    	ans2=qm(k,shu[i],lzy2);
	    	ans3=qm(k,shu[i],lzy3);
		}
		else
		{
			ans1=(ans1-qm(k,shu[i],lzy1)+lzy1*2)%lzy1;
			ans2=(ans2-qm(k,shu[i],lzy2)+lzy2*2)%lzy2;
			ans3=(ans3-qm(k,shu[i],lzy3)+lzy3*2)%lzy3;
		}
		cout<<ans1<<"\n";
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
const int MOD1 = 1e9+7;
const int MOD2 = 1e9+9;
const int MOD3 = 1433223;
const int MOD4 = 998244353;
int T;
int n,p;
int a[N];
int ans1,ans2,ans3,ans4;
inline int qpow(int x,int b,int MOD)
{
	int ret=1;
	int base=x;
	while(b)
	{
		if(b&1) ret=ret*base%MOD;
		base=base*base%MOD;
		b>>=1;
	}
	return ret;
}
signed main()
{
	scanf("%lld",&T);
	while(T--)
	{
		ans1=ans2=ans3=ans4=0;
		scanf("%lld%lld",&n,&p);
		for(int i=1;i<=n;++i) scanf("%lld",&a[i]);
		if(p==1)
		{
			printf("%lld\n",n%2);
			continue;
		}
		sort(a+1,a+n+1);
		for(int i=n;i>=1;--i)
		{
			if(!ans1&&!ans2&&!ans3&&!ans4)
			{
				ans1=qpow(p,a[i],MOD1);
				ans2=qpow(p,a[i],MOD2);
				ans3=qpow(p,a[i],MOD3);
				ans4=qpow(p,a[i],MOD4);
			}
			else
			{
				ans1=(ans1-qpow(p,a[i],MOD1)+MOD1)%MOD1;
				ans2=(ans2-qpow(p,a[i],MOD2)+MOD2)%MOD2;
				ans3=(ans3-qpow(p,a[i],MOD3)+MOD3)%MOD3;
				ans4=(ans4-qpow(p,a[i],MOD4)+MOD4)%MOD4;
			}
		}
		printf("%lld\n",ans1);
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

inline int read() {
	char c = getchar(); int x = 0, f = 1;
	while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); }
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * f;
}

const int N = 1000005, P1 = 1e9 + 7, P2 = 998244353, P3 = 999998639;

int a[N];

inline void upd(int &x, int y, int mod) {
	x = (x + y >= mod) ? x + y - mod : x + y;
}
inline int fpow(int a, int b, int mod) {
	ll ret = 1;
	for (; b; b >>= 1, a = (ll)a * a % mod)
		if (b & 1) ret = (ll)ret * a % mod;
	return ret;
}

bool cmp(int a, int b) {
	return a > b;
}

int main() {
	int T = read();
	while (T--) {
		int n = read(), p = read();
		for (int i = 1; i <= n; i++) a[i] = read();
		sort(a + 1, a + n + 1, cmp);
		int ans = 0, wt1 = 0, wt2 = 0;
		for (int i = 1; i <= n; i++)
			if (ans == 0 && wt1 == 0 && wt2 == 0)
				upd(ans, fpow(p, a[i], P1), P1), upd(wt1, fpow(p, a[i], P2), P2), upd(wt2, fpow(p, a[i], P3), P3);
			else
				upd(ans, P1 - fpow(p, a[i], P1), P1), upd(wt1, P2 - fpow(p, a[i], P2), P2), upd(wt2, P3 - fpow(p, a[i], P3), P3);
		printf("%d\n", ans);
	}
	return 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
inline int read()
{
	int GYH,LZY;
	GYH=0,LZY=1;
	char aqx;
	aqx=getchar();
	while(aqx<'0'||aqx>'9')
	{
		if(aqx=='-')LZY=-1;
		aqx=getchar();
	}
	while(aqx>='0'&&aqx<='9')
	{
		GYH=(GYH<<1)+(GYH<<3)+aqx-'0';
		aqx=getchar();
	}
	return GYH*LZY;
}
const int maxn=5e5+5;
const int lzy1=1e9+7;
const int lzy2=998244353;
int yzakioi;
int n,k;
int shu[maxn];
int ans1,ans2;
inline bool cmp(int x,int y){return x>y;}
int qm(int x,int y,int lzy)
{
	if(!y)return 1;
	int jie=qm(x,y>>1,lzy);
	if(y%2)return (jie*jie*x)%lzy;
	return (jie*jie)%lzy;
}
signed main()
{
	yzakioi=read();
	while(yzakioi--)
	{
		n=read(),k=read();
		for(int i=1;i<=n;i++)
		shu[i]=read();
		sort(shu+1,shu+1+n,cmp);
		ans1=ans2=0;
		for(int i=1;i<=n;i++)
		if(!ans1&&!ans2)
		{
	    	ans1=qm(k,shu[i],lzy1);
	    	ans2=qm(k,shu[i],lzy2);
		}
		else
		{
			ans1=(ans1-qm(k,shu[i],lzy1)+lzy1)%lzy1;
			ans2=(ans2-qm(k,shu[i],lzy2)+lzy2)%lzy2;
		}
		cout<<ans1<<"\n";
	}
}


#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define int long long
#define gmax(x,y) x=max(x,y)
#define gmin(x,y) x=min(x,y)
#define F first
#define S second
#define P pair
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define rep(i,a,b) for(int i=a;i<b;i++)
#define V vector
#define RE return
#define ALL(a) a.begin(),a.end()
#define MP make_pair
#define PB emplace_back
#define PF emplace_front
#define FILL(a,b) memset(a,b,sizeof(a))
#define lwb lower_bound
#define upb upper_bound
using namespace std;
int m[1000005],mt[1000005];
int a[1000005],mod=1e9+7;
int POW(int x,int y){
	if(!y)RE 1;
	int t=POW(x,y/2);
	t=t*t%mod;
	if(y&1){
		RE t*x%mod;
	}else RE t;
}
void solve(){
	int n,p;
	cin>>n>>p;
	V<int> v;
	FOR(i,1,n)cin>>a[i],v.PB(a[i]);
	if(p==1){
		cout<<(n&1)<<'\n';RE;
	}
	FOR(i,1,n)m[a[i]]++,mt[a[i]]++;
	sort(ALL(v));
	v.erase(unique(ALL(v)),v.end());
	for(int i=v.size()-1;i>=0;i--){
		if(m[v[i]]&1){
			int last=1;
			FOR(j,1,v[i]){
				last*=p;
				if(m[v[i]-j]<last){
					last-=m[v[i]-j];
					m[v[i]-j]=0;
				}else{
					m[v[i]-j]-=last;last=0;
					break;
				}
				if(last>=1000000)break;
			}
			if(last){
				int ans=POW(p,v[i]);
				rep(j,0,i){
					ans=(ans-POW(p,v[j])*mt[v[j]]%mod+mod)%mod;
				}
				cout<<ans<<'\n';
				for(auto u:v)m[u]=mt[u]=0;
				RE;
			}
		}
	}
	for(auto u:v)m[u]=mt[u]=0;
	cout<<0<<'\n';
}
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin>>t;
	while(t--)solve();
	RE 0;
}


#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define int long long
using namespace std;
const int maxn=1e6+5;
const int lzy1=1e9+7;
const int lzy2=998244353;
int yzakioi;
int n,k;
int shu[maxn];
int ans1,ans2;
inline bool cmp(int x,int y){return x>y;}
inline int qm(int x,int y,int lzy)
{
	int jie=1;
	for(;y;y>>=1,x=(x*x)%lzy)
	if(y&1)
	jie*=x,jie%=lzy;
	return jie;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin>>yzakioi;
	while(yzakioi--)
	{
		cin>>n>>k;
		for(int i=1;i<=n;i++)
		cin>>shu[i];
		if(k==1)
		{
			cout<<n%2<<"\n";
			continue;
		}
		sort(shu+1,shu+1+n,cmp);
		ans1=ans2=0;
		for(int i=1;i<=n;i++)
		if(!ans1&&!ans2)
		{
	    	ans1=qm(k,shu[i],lzy1);
	    	ans2=qm(k,shu[i],lzy2);
		}
		else
		{
			ans1=(ans1-qm(k,shu[i],lzy1)+lzy1*2)%lzy1;
			ans2=(ans2-qm(k,shu[i],lzy2)+lzy2*2)%lzy2;
		}
		cout<<ans1<<"\n";
	}
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
ll k[10000050];
const ll mod= 1000000007;
const ll Mod= 1000000003;
ll n,p;
ll qpow(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    if(k&1)
        return n*(qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    else
    {
        return (qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    }

}
bool cmp(ll a,ll b)
{ return a>b;}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%lld%lld",&n,&p);
        for(int i=1;i<=n;i++)
           scanf("%lld",&k[i]);
        int j=1;
        sort(k+1,k+1+n,cmp);
        ll res=0;ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!ans&&!res)
            {
                ans+=qpow(p,k[i]);
                res+=qpow(p,k[i]);
            }
            else
            {
                ans=((ans-qpow(p,k[i]))%mod+mod)%mod;
                res=((res-qpow(p,k[i]))%Mod+Mod)%Mod;
            }

        }
        cout<<ans<<endl;
    }
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#include <map>
#include <list>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <stack>
#include <set>
#pragma GCC optimize(3 , "Ofast" , "inline")
using namespace std ;
#define ios ios::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define x first
#define y second
typedef long long ll ;
const double esp = 1e-6 , pi = acos(-1) ;
typedef pair<int , int> PII ;
const int N = 1e6 + 10 , INF = 0x3f3f3f3f , mod = 1e9 + 7 , Mod = 1e9 + 3;
ll in()
{
  ll x = 0 , f = 1 ;
  char ch = getchar() ;
  while(!isdigit(ch)) {if(ch == '-') f = -1 ; ch = getchar() ;}
  while(isdigit(ch)) x = x * 10 + ch - 48 , ch = getchar() ;
  return x * f ;
}
ll qmi(ll a , ll b , ll mod)
{
  ll res = 1 ;
  while(b)
   {
     if(b & 1) res = res * a % mod ;
     a = a * a % mod ;
     b >>= 1;
   }
   return res ;
}
ll k[N] ;
void work()
{
  int n = in() , p = in() ;
  for(int i = 1; i <= n ;i ++ ) k[i] = in() ;
  if(p == 1)
   {
     cout << (n % 2) << endl ;
     return ;
   }
  sort(k + 1 , k + n + 1) ;
  reverse(k + 1 ,k + n + 1) ;
  ll res = 0 , ans = 0 ;
  for(int i = 1; i <= n ;i ++ )
     if(!ans && !res) ans += qmi(p , k[i] , mod) , res += qmi(p , k[i] , Mod) ;
     else
       ans = ((ans - qmi(p , k[i] , mod))% mod + mod) % mod ,
       res = ((res - qmi(p , k[i] , Mod))% Mod + Mod) % Mod ;
  cout << ans << endl ;
  return ;

}
int main()
{
  int n = in() ;
  while(n --) work() ;
  return 0 ;
}
/*
*/


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
int vis[1000050];
ll k[10000050];
const ll mod= 1000000007;
ll n,p;
ll qpow(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    if(k&1)
        return n*(qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    else
    {
        return (qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    }

}
bool cmp(ll a,ll b)
{ return a>b;}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>p;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
            cin>>k[i];
            vis[k[i]]++;
        }
        int j=1;
        sort(k+1,k+1+n,cmp);
        ll res=0;ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!ans&&!res)
            {
                ans+=qpow(p,k[i]);
                res+=qpow(p,k[i]);
            }
            else
            {
                ans=((ans-qpow(p,k[i]))%mod+mod)%mod;
                res=((res-qpow(p,k[i]))%mod+mod)%mod;
            }

        }
        cout<<ans<<endl;
    }
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
int vis[1000050];
ll k[10000050];
const ll mod= 1000000007;
ll ans[2];
ll n,p;
ll qpow(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    if(k&1)
        return n*(qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    else
    {
        return (qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    }

}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        cin>>n>>p;
        memset(vis,0,sizeof(vis));
        memset(ans,0,sizeof(ans));
        for(int i=1;i<=n;i++)
        {
            cin>>k[i];
            vis[k[i]]++;
        }
        int j=1;
        sort(k+1,k+1+n);
        while(j<=n)
        {
            ans[j&1]=(ans[j&1]+qpow(p,k[j]))%mod;
            j++;
        }
        cout<<abs(ans[0]-ans[1])<<endl;
    }
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
int vis[1000050];
ll k[10000050];
const ll mod= 1000000007;
ll n,p;
ll qpow(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    if(k&1)
        return n*(qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    else
    {
        return (qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    }

}
bool cmp(ll a,ll b)
{ return a>b;}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%lld%lld",&n,&p);
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
        {
           scanf("%lld",&k[i]);
            vis[k[i]]++;
        }
        int j=1;
        sort(k+1,k+1+n,cmp);
        ll res=0;ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!ans&&!res)
            {
                ans+=qpow(p,k[i]);
                res+=qpow(p,k[i]);
            }
            else
            {
                ans=((ans-qpow(p,k[i]))%mod+mod)%mod;
                res=((res-qpow(p,k[i]))%mod+mod)%mod;
            }

        }
        cout<<ans<<endl;
    }
}


#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
#define ll long long
ll k[10000050];
const ll mod= 1000000007;
ll n,p;
ll qpow(ll n,ll k)
{
    if(k==0)
        return 1;
    if(k==1)
        return n%mod;
    if(k&1)
        return n*(qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    else
    {
        return (qpow(n,k>>1)%mod)*(qpow(n,k>>1)%mod)%mod;
    }

}
bool cmp(ll a,ll b)
{ return a>b;}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%lld%lld",&n,&p);
        for(int i=1;i<=n;i++)
           scanf("%lld",&k[i]);
        int j=1;
        sort(k+1,k+1+n,cmp);
        ll res=0;ll ans=0;
        for(int i=1;i<=n;i++)
        {
            if(!ans&&!res)
            {
                ans+=qpow(p,k[i]);
                res+=qpow(p,k[i]);
            }
            else
            {
                ans=((ans-qpow(p,k[i]))%mod+mod)%mod;
                res=((res-qpow(p,k[i]))%mod+mod)%mod;
            }

        }
        cout<<ans<<endl;
    }
}


#include <bits/stdc++.h>
using namespace std;

const long long MOD = 1000000007;

int mpow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        res %= MOD;
    }
    return res;
}

int pow(const int x, const int y, bool& flag) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= MOD) {
            flag = true;
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= MOD) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta, flag), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            res %= MOD;
        }
        base = base*base%MOD;
        y >>= 1;
    }
    return res;
}

int pow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= INF) {
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (flag) {
        return 0;
    }
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const long long INF = 1000010;
const long long MOD = 1000000007;

int mpow(const int x, int y) {
    long long res = 1;
    long long base = x;
    while (y) {
        if (y&1) {
            res *= base;
            res %= MOD;
        }
        base <<= 1;
        y >>= 1;
    }
    return res;
}

int pow(const int x, const int y) {
    long long res = 1;
    for (int i = 0; i < y; ++i) {
        res *= x;
        if (res >= INF) {
            return res;
        }
    }
    return res;
}

int mmulti(const int x, const int y) {
    if (0 == x) {
        return 0;
    }
    return (long long)x*y%MOD;
}

int multi(const int x, const int y, bool& flag) {
    if (flag) {
        return 0;
    }
    if (0 == x) {
        return 0;
    }
    auto res = (long long)x*y;
    if (res >= INF) {
        flag = true;
    }
    return res;
}

int msub(const int x, const int y) {
    return (x+y*(MOD-1))%MOD;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, p;
        cin >> n >> p;
        map<int, int> x2n;
        for (int i = 0; i < n; ++i) {
            int k;
            cin >> k;
            x2n[k]++;
        }
        int mx = 0;
        int x = 0;
        bool flag = false;
        for (auto iter = x2n.rbegin(); iter != x2n.rend(); ++iter) {
            if (iter != x2n.rbegin()) {
                int k = iter->first;
                --iter;
                int delta = iter->first-k;
                ++iter;
                mx = mmulti(mx, mpow(p, delta));
                if (!flag) {
                    x = multi(x, pow(p, delta), flag);
                }
            }
            if (flag) {
                mx = msub(mx, iter->second);
            } else if (x <= iter->second) {
                if ((iter->second-x)%2) {
                    mx = x = 1;
                } else {
                    mx = x = 0;
                }
            } else {
                x -= iter->second;
                mx = x;
            }
        }
        cout << mmulti(mx, mpow(p, x2n.begin()->first)) << endl;
    }

    return 0;
}