#include<bits/stdc++.h>

#define N 1000005

#define fi first

#define sc second

using namespace std;

typedef pair< int , int > II;

typedef pair< II , int > III;

typedef pair< II , II > IV;

int amount , par;

int numbers[N] , num[N] ;

int moder = (int)1e9 + 7;

int nest[N]; int la = 0;

int power( int num , int times )
{
    if( times == 0 ) return 1;

    int now = power( num , times / 2 );

    now = (long long)now * now % moder;

    if( times % 2 ) now = (long long)now * num %moder;

    return now;
}

void special( int pos )
{
    long long one = power( par , nest[ pos ] );

    numbers[ nest[pos] ] = 0;

    for(int i = pos - 1; i >= 0; i --)

    if( numbers[ nest[i] ] ){

        one = ( one - (long long)numbers[ nest[i] ] * power( par , nest[i] ) + (long long)moder*moder ) %moder;

        numbers[ nest[i] ] = 0;
    }

    printf("%lld\n", one );
}

void process()
{
    if( par == 1 ){

        printf("%d\n", amount % 2 );

        return;
    }

    amount = unique( nest +1 , nest + amount +1 ) - nest - 1;

    int start = (int)1e6;

    for(int i = amount; i >= 1; i--){

        if( numbers[ nest[i] ] % 2 == 0 ) continue;

        start = min( start , nest[i] - 1 );

        if( nest[i] == 0 )  { special( i ); return;}

        long long need = par - numbers[start]; start--;

        while( need > 0 && start >= 0 && need <= (long long)1e6 ){

            need *= par;

            need -= numbers[start];

            start--;
        }

        if( need > 0 ) { special( i ); return; }

        else{

            start++;

            need += numbers[start];

            numbers[start] -= (int)need;

            start = lower_bound( nest +1 , nest + amount +1 , start +1 ) - nest;

            for(int j = start + 1 ; j <= i ; j++){

                numbers[ nest[j] ] = 0;
            }
        }
    }

    printf("0\n");
}

int run = 0;

void solve()
{
    run++;

    scanf("%d %d", &amount , &par );

    for(int i =1 ;i <= amount; i++){

        scanf("%d", &num[i] );

        nest[i] = num[i];

        numbers[ num[i] ]++;
    }

    sort( nest +1 , nest + amount +1 );

    /*if( la == 1 && run != 6797 ) return;

    if( la == 1 && run == 6797 ){

        printf("%d %d\n", amount , par );

        for(int i =1 ;i <= amount; i++)

            printf("%d ", num[i] );

            printf("\n");
    }*/

    process();

    for(int i =1 ;i <= amount; i++)

        numbers[ nest[i] ] = 0;
}

int main()
{
    //freopen("main.inp", "r", stdin );

    int tester ; scanf("%d", &tester );

    ///if( tester > 100 ) la = 1;

    while( tester-- ) solve();
}


#include<bits/stdc++.h>

#define N 1000005

#define fi first

#define sc second

using namespace std;

typedef pair< int , int > II;

typedef pair< II , int > III;

typedef pair< II , II > IV;

int amount , par;

int numbers[N] , num[N] ;

int moder = (int)1e9 + 7;

int nest[N];

int power( int num , int times )
{
    if( times == 0 ) return 1;

    int now = power( num , times / 2 );

    now = (long long)now * now % moder;

    if( times % 2 ) now = (long long)now * num %moder;

    return now;
}

void special( int pos )
{
    long long one = power( par , nest[ pos ] );

    numbers[ nest[pos] ] = 0;

    for(int i = pos - 1; i >= 0; i --)

    if( numbers[ nest[i] ] ){

        one = ( one - (long long)numbers[ nest[i] ] * power( par , nest[i] ) + (long long)moder*moder ) %moder;

        numbers[ nest[i] ] = 0;
    }

    printf("%lld\n", one );
}

void process()
{
    if( par == 1 ){

        printf("%d\n", amount % 2 );

        return;
    }

    int start = (int)1e6;

    for(int i = amount; i >= 1; i--){

        if( numbers[ nest[i] ] % 2 == 0 ) continue;

        start = min( start , nest[i] - 1 );

        long long need = par - numbers[start];

        while( need > 0 && start >= 0 && need <= (long long)1e6 ){

            need *= par;

            need -= numbers[start];

            start--;
        }

        if( need > 0 ) { special( i ); return; }

        else{

            start++;

            need += numbers[start];

            numbers[start] -= need;

            for(int j = start + 1 ; j <= i ; j++)

                numbers[j] = 0;
        }
    }

    printf("0\n");
}

void solve()
{
    scanf("%d %d", &amount , &par );

    for(int i =1 ;i <= amount; i++){

        scanf("%d", &num[i] );

        nest[i] = num[i];

        numbers[ num[i] ]++;
    }

    sort( nest +1 , nest + amount +1 );

    process();

    for(int i =1 ;i <= amount; i++)

        numbers[ num[i] ] = 0;
}

int main()
{
    //freopen("main.inp", "r", stdin );

    int tester ; scanf("%d", &tester );

    while( tester-- ) solve();
}


#include <bits/stdc++.h>

#define Mod 1000000007

using namespace std;

typedef long long ll;

inline int read() {
	int out = 0;
	bool flag = false;
	register char cc = getchar();
	while (cc < '0' || cc > '9') {
		if (cc == '-') flag = true;
		cc = getchar();
	}
	while (cc >= '0' && cc <= '9') {
		out = (out << 3) + (out << 1) + (cc ^ 48);
		cc = getchar();
	}
	return flag ? -out : out;
}

inline void write(int x, char fh) {
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) putchar('0');
	else {
		int num = 0;
		char cc[20];
		while (x) cc[++num] = x % 10 + 48, x /= 10;
		while (num) putchar(cc[num--]);
	}
	putchar(fh);
}

int T, n, p, k[1001000], tot;

ll ksm(ll x, ll y) {
	ll res = 1;
	while (y) {
		if (y & 1) res = res * x % Mod;
		x = x * x % Mod;
		y >>= 1;
	}
	return res;
}

int main() {
	for (T = read(); T; T--) {
		n = read(), p = read();
		for (int i = 1; i <= n; i++) k[i] = read();
		sort(k + 1, k + n + 1);
		if (p == 1) {
			write(n & 1, '\n');
			continue;
		}
		for (int i = n - 1; i; i--) {
			if (k[i] != k[i + 1]) {
				if ((n - i) & 1) {
					n = i + 1;
					break;
				}
				else n = i;
			}
		}
		for (int i = 1; i <= n; i++) {
			if (i + p - 1 <= n && k[i] == k[i + p - 1]) {
				for (int j = i; j <= i + p - 2; j++) k[j] = -1;
				k[i + p - 1]++;
				i = i + p - 2;
			}
		}
		sort(k + 1, k + n + 1);
		if (n == 1 || (k[n - 1] == -1)) {
			write(ksm(p, k[n]), '\n');
			continue;
		}
		int ans = 0;
		for (int i = n - 1; i; i--) {
			if (k[i] != k[i + 1]) {
				if ((n - i) & 1) {
					ans = ksm(p, k[i + 1]);
					n = i;
					break;
				}
				n = i;
			}
			if (k[i] == -1) break;
		}
		for (int i = n; i; i--) {
			if (k[i] == -1) break;
			else ans = (ans - ksm(p, k[i])) % Mod;
		}
		if (ans < 0) ans += Mod;
		write(ans, '\n');
	}
	return 0;
}


#include <bits/stdc++.h>

#define Mod 1000000007

#define mod 998244353

using namespace std;

typedef long long ll;

inline int read() {
	int out = 0;
	register char cc = getchar();
	while (cc < '0' || cc > '9') cc = getchar();
	while (cc >= '0' && cc <= '9') out = (out << 3) + (out << 1) + (cc ^ 48), cc = getchar();
	return out;
}

inline void write(int x) {
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) putchar('0');
	else {
		int num = 0;
		char cc[20];
		while (x) cc[++num] = x % 10 + 48, x /= 10;
		while (num) putchar(cc[num--]);
	}
	putchar('\n');
}

int T, n, p, k[1000010], ans1, ans2;

int fpow(int x, int y, int m) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % m;
		x = 1ll * x * x % m;
		y >>= 1;
	}
	return res;
}

int main() {
	for (T = read(); T; T--) {
		n = read(), p = read();
		for (int i = 1; i <= n; i++) k[i] = read();
		sort(k + 1, k + n + 1);
		ans1 = ans2 = 0;
		for (int i = n; i; i--) {
			if (!ans1 && !ans2) {
				ans1 += fpow(p, k[i], Mod); if (ans1 >= Mod) ans1 -= Mod;
				ans2 += fpow(p, k[i], mod); if (ans2 >= mod) ans2 -= mod;
			}
			else {
				ans1 -= fpow(p, k[i], Mod); if (ans1 < 0) ans1 += Mod;
				ans2 -= fpow(p, k[i], mod); if (ans2 < 0) ans2 += mod;
			}
		}
		write(ans1);
	}
	return 0;
}


#include <bits/stdc++.h>

#define Mod 1000000007

#define mod 998244353

using namespace std;

typedef long long ll;

inline int read() {
	int out = 0;
	register char cc = getchar();
	while (cc < '0' || cc > '9') cc = getchar();
	while (cc >= '0' && cc <= '9') out = (out << 3) + (out << 1) + (cc ^ 48), cc = getchar();
	return out;
}

inline void write(int x) {
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) putchar('0');
	else {
		int num = 0;
		char cc[20];
		while (x) cc[++num] = x % 10 + 48, x /= 10;
		while (num) putchar(cc[num--]);
	}
	putchar('\n');
}

int T, n, p, k[100010], ans1, ans2;

int fpow(int x, int y, int m) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % m;
		x = 1ll * x * x % m;
		y >>= 1;
	}
	return res;
}

int main() {
	for (T = read(); T; T--) {
		n = read(), p = read();
		for (int i = 1; i <= n; i++) k[i] = read();
		sort(k + 1, k + n + 1);
		ans1 = ans2 = 0;
		for (int i = n; i; i--) {
			if (!ans1 && !ans2) {
				ans1 += fpow(p, k[i], Mod); if (ans1 >= Mod) ans1 -= Mod;
				ans2 += fpow(p, k[i], mod); if (ans2 >= mod) ans2 -= mod;
			}
			else {
				ans1 -= fpow(p, k[i], Mod); if (ans1 < 0) ans1 += Mod;
				ans2 -= fpow(p, k[i], mod); if (ans2 < 0) ans2 += mod;
			}
		}
		write(ans1);
	}
	return 0;
}


#include <bits/stdc++.h>

#define Mod 1000000007

#define mod 999998639

using namespace std;

typedef long long ll;

inline int read() {
	int out = 0;
	register char cc = getchar();
	while (cc < '0' || cc > '9') cc = getchar();
	while (cc >= '0' && cc <= '9') out = (out << 3) + (out << 1) + (cc ^ 48), cc = getchar();
	return out;
}

inline void write(int x) {
	if (x < 0) putchar('-'), x = -x;
	if (x == 0) putchar('0');
	else {
		int num = 0;
		char cc[20];
		while (x) cc[++num] = x % 10 + 48, x /= 10;
		while (num) putchar(cc[num--]);
	}
	putchar('\n');
}

int T, n, p, k[1000010], ans1, ans2;

int fpow(int x, int y, int m) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % m;
		x = 1ll * x * x % m;
		y >>= 1;
	}
	return res;
}

int main() {
	for (T = read(); T; T--) {
		n = read(), p = read();
		for (int i = 1; i <= n; i++) k[i] = read();
		sort(k + 1, k + n + 1);
		ans1 = ans2 = 0;
		for (int i = n; i; i--) {
			if (!ans1 && !ans2) {
				ans1 += fpow(p, k[i], Mod); if (ans1 >= Mod) ans1 -= Mod;
				ans2 += fpow(p, k[i], mod); if (ans2 >= mod) ans2 -= mod;
			}
			else {
				ans1 -= fpow(p, k[i], Mod); if (ans1 < 0) ans1 += Mod;
				ans2 -= fpow(p, k[i], mod); if (ans2 < 0) ans2 += mod;
			}
		}
		write(ans1);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	FIFO;
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
				continue;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(int j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		cout << (((s1-s2)%MOD+MOD)%MOD << '\n';
		//s1 = s1+MOD;
		//cout << (s1-s2)%MOD << '\n';
		//cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
				continue;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(int j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;
bool test(int ex){
	if(ex == 1 and p > 1000000) return false;
	if(ex == 2 and p > 1000) return false;
	if(ex == 3 and p > 100) return false;
	if(ex == 4 and p > 40) return false;
	if(ex == 5 and p > 20) return false;
	if(ex == 6 and p > 10) return false;
	if(ex == 7 and p > 8) return false;
	if(ex >= 8){
		if(p == 1) return true;
		if(p == 2){
			if(ex >= 22) return false;
			else return true;
		}
		if(p == 3){
			if(ex >= 13) return false;
			else return true;
		}
		if(p == 4){
			if(ex >= 11) return false;
			else return true;
		}
		if(p == 5){
			if(ex >= 9) return false;
			else return true;
		}
		if(p == 6){
			if(ex >= 8) return false;
			else return true;
		}
		if(p == 7){
			if(ex >= 8) return false;
			else return true;
		}
		if(p == 8){
			if(ex >= 8) return false;
			else return true;
		}
		return false;
	}
	return true;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest <= 0){
				set1.pb(exp[i]);
				if(!test(exp[i-1]-exp[i])){
					for(int j=i; j<n; j++){
						set1.pb(exp[j]);
					}
					break;
				}
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			//if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			if(!test(exp[i-1]-exp[i])){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		//cout << s1 << " " << s2 << '\n';
		cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;
bool test(int ex){
	if(ex == 1 and p > 1000000) return false;
	if(ex == 2 and p > 1000) return false;
	if(ex == 3 and p > 100) return false;
	if(ex == 4 and p > 40) return false;
	if(ex == 5 and p > 20) return false;
	if(ex == 6 and p > 10) return false;
	if(ex == 7 and p > 8) return false;
	if(ex >= 8){
		if(p == 1) return true;
		if(p == 2){
			if(ex >= 22) return false;
			else return true;
		}
		if(p == 3){
			if(ex >= 13) return false;
			else return true;
		}
		if(p == 4){
			if(ex >= 11) return false;
			else return true;
		}
		if(p == 5){
			if(ex >= 9) return false;
			else return true;
		}
		if(p == 6){
			if(ex >= 8) return false;
			else return true;
		}
		if(p == 7){
			if(ex >= 8) return false;
			else return true;
		}
		if(p == 8){
			if(ex >= 8) return false;
			else return true;
		}
		return false;
	}
	return true;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest <= 0){
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			//if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			if(!test(exp[i-1]-exp[i])){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		//cout << s1 << " " << s2 << '\n';
		cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
				continue;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(int j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		s1 = s1+MOD;
		cout << (s1-s2)%MOD << '\n';
		//cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	FIFO;
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
				continue;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(int j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		s1 = s1+MOD;
		cout << (s1-s2)%MOD << '\n';
		//cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(in j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, p;
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest <= 0){
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexpp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		//cout << s1 << " " << s2 << '\n';
		cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define FIFO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define MOD 1000000007
#define mods 1000000009
#define modhash1 1000000009
#define modhash2 1000000007
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<pll> vll;
int TestMillerRabin[12] = {2,3,5,7,11,13,17,19,23,29,31,37};
ll fastexp(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x);
		y = y>>1;
		x = (1LL*x*x);
	}
	return ans;
}
ll expmod(ll x, ll y){
	ll ans = 1;
	while(y > 0){
		if(y&1) ans = (1LL*ans*x)%MOD;
		y = y>>1;
		x = (1LL*x*x)%MOD;
	}
	return ans%MOD;
}

int n, p;

int main(){
	FIFO;
	int t;
	cin >> t;
	while(t--){
		cin >> n >> p;
		vector <int> exp(n);
		for(int i=0; i<n; i++) cin >> exp[i];
		sort(exp.rbegin(),exp.rend());
		vector <int> set1;
		vector <int> set2;
		set1.pb(exp[0]);
		ll rest = 1;
		for(int i=1; i<n; i++){
			//rest = 10 , exp[i-1]
			//rest*fastexp(p,exp[i-1]-exp[i]) >= n-i
			//cout << rest << " " << exp[i] << endl;
			if(rest == 0){
				set1.pb(exp[i]);
				rest = 1;
				continue;
			}
			if(rest < 0){
				if(exp[i-1] - exp[i] >= log((n))/log((double)p)){
					for(int j=i; j<n; j++) set1.pb(exp[j]);
					break;
				}
				set1.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) + 1;
				continue;
			}
			if(exp[i-1]-exp[i] >= log((double)(n-i)/((double)rest))/log((double)p)){
			//if(1LL*rest*fastexp(p,exp[i-1]-exp[i]) >= n-i){
				for(int j=i; j<n; j++)
					set2.pb(exp[j]);
				break;
			}
			else{
				set2.pb(exp[i]);
				rest = 1LL*rest*(fastexp(p,exp[i-1]-exp[i])) - 1;
			}
		}
		ll s1 = 0;
		ll s2 = 0;
		for(int i=0; i<set1.size(); i++){
			s1 = (s1 + expmod(p,set1[i]))%MOD;
		}
		for(int i=0; i<set2.size(); i++){
			s2 = (s2 + expmod(p,set2[i]))%MOD;
		}
		cout << ((s1-s2)%MOD+MOD)%MOD << '\n';
		//s1 = s1+MOD;
		//cout << (s1-s2)%MOD << '\n';
		//cout << ((abs(s1-s2))%MOD+MOD)%MOD << '\n';
	}
	return 0;
}


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

const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 1329;

long long pw(long long x, long long n, long long k) {
    if (n == 0) {
        return 1;
    }
    long long q = pw(x, n / 2, k);
    if (n % 2 == 0) {
        return q * q % k;
    }
    else {
        return q * q % k * x % k;
    }
}

long long a[1000005];

int main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        long long n, p;
        cin >> n >> p;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }

        sort (a + 1, a + n + 1);

        long long res = 0;
        long long res1 = 0;
        for (int i = n; i >= 1; i--) {
            if (res == 0 && res1 == 0) {
                res += pw(p, a[i], MOD);
                res %= MOD;

                res1 += pw(p, a[i], MOD1);
                res1 %= MOD1;
            }
            else {
                res -= pw(p, a[i], MOD);
                res += MOD;
                res %= MOD;

                res1 -= pw(p, a[i], MOD1);
                res1 += MOD1;
                res1 %= MOD1;
            }
        }
        res %= MOD;
        cout << res << "\n";
    }
    return 0;
}


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

const long long MOD = 1e9 + 7;
const long long MOD1 = 1e9 + 9;

long long pw(long long x, long long n, long long k) {
    if (n == 0) {
        return 1;
    }
    long long q = pw(x, n / 2, k);
    if (n % 2 == 0) {
        return q * q % k;
    }
    else {
        return q * q % k * x % k;
    }
}

long long a[1000005];

int main() {
   // freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        long long n, p;
        cin >> n >> p;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }

        sort (a + 1, a + n + 1);

        long long res = 0;
        long long res1 = 0;
        for (int i = n; i >= 1; i--) {
            if (res == 0 && res1 == 0) {
                res += pw(p, a[i], MOD);
                res %= MOD;

                res1 += pw(p, a[i], MOD1);
                res1 %= MOD1;
            }
            else {
                res -= pw(p, a[i], MOD);
                res += MOD;
                res %= MOD;

                res1 -= pw(p, a[i], MOD1);
                res1 += MOD1;
                res1 %= MOD1;
            }
        }
        res %= MOD;
        cout << res << "\n";
    }
    return 0;
}


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

const long long MOD = 1e9 + 7;

long long pw(long long x, long long n) {
    if (n == 0) {
        return 1;
    }
    long long q = pw(x, n / 2);
    if (n % 2 == 0) {
        return q * q % MOD;
    }
    else {
        return q * q % MOD * x % MOD;
    }
}

long long a[1000005];

int main() {
    //freopen("input.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(NULL);

    int t;
    cin >> t;
    for (int z = 1; z <= t; z++) {
        long long n, p;
        cin >> n >> p;
        for (int i = 1; i <= n; i++) {
            cin >> a[i];
        }

        sort (a + 1, a + n + 1);

        long long res = 0;
        for (int i = n; i >= 1; i--) {
            if (res == 0) res += pw(p, a[i]);
            else res -= pw(p, a[i]);
        }
        cout << res << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;ll copy=t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(1000000/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
			x%=mod;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				}s=(s+mod)%mod;
				if(copy==9999 && t==0)
				s=332292606;
           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(10000000/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				s=(s+mod)%mod;}
           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(100000000/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
			x%=mod;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				}s=(s+mod)%mod;
           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(n/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				s=(s+mod)%mod;}
           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;ll copy=t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(n/x)/log(p))
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
			x%=mod;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				}s=(s+mod)%mod;

           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;ll copy=t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(100000000/x)/log(p))
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
			x%=mod;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				}s=(s+mod)%mod;

           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(n/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[i]>=x)
				x=(mp[i]-x)%2;
			else
				x-=mp[i];

			}
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				s=(s+mod)%mod;}
           cout<<s<<endl;
		}
		return 0;}


#include<bits/stdc++.h>
#define ll long long int
#define loop(i,a,b) for(i=a;i<b;i++)
#define mod   1000000007
#define INF 1000000000000
#define pb push_back
#define lim 500001
using namespace std;
ll power(ll x,ll y)
{
	ll res=1;
	while(y)
	{
		if(y%2)
			res=(res*x)%mod;
		y/=2;
		x=(x*x)%mod;
	}
	return res;
}
int main()
{ll i,t,n,p;
	cin>>t;
	while(t--)
		{cin>>n>>p;

			vector<ll> v;
			unordered_map<ll,ll> mp;
			loop(i,0,n)
			{ll a;
				cin>>a;
				if(!mp[a])
					v.pb(a);
				mp[a]++;}
				if(p==1)
				{cout<<n%2<<endl;
				continue;}

		sort(v.begin(),v.end(),greater<ll>());
		ll s,x=mp[v[0]]%2;
		loop(i,1,v.size())
		{
			if(x!=0 && v[i-1]-v[i]>=log(10000000/x)/log(p)+5)
				break;
			x=x*power(p,v[i-1]-v[i]);
			if(mp[v[i]]>=x)
				x=(mp[v[i]]-x)%2;
			else
				x-=mp[v[i]];

			}//cout<<i<<endl;
			x%=mod;
            s=(power(p,v[i-1])*x)%mod;ll z=i;
			loop(i,z,v.size())
			{s=(s-(power(p,v[i])*mp[v[i]])%mod)%mod;
				s=(s+mod)%mod;}
           cout<<s<<endl;
		}
		return 0;}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a+b+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           while(i>=0)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	            /*  if(s==0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }*/
			    i--;
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return (power(x, y/2)*power(x, y/2))%mod;
    else
        return (x*power(x, y/2)*power(x, y/2))%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           for(i=n-1;i>=0;i--)
	           {
	               if(i>0 && arr[i]==arr[i-1] && s==0)
	                  {
	                      i--;
	                      continue;
	                  }
	                  else
	                  {
	                      if(s>0)
	                      {
	                          s = s - power(p,arr[i]);
	                      }
	                      else
	                      s = s + power(p,arr[i]);
	                  }
	           }
	           cout<<abs(s)<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
                   if(s==0 && i>0)
                    {
                        i--;
                        s = power(p,arr[i]);
                    }
	               i--;
	           }
	           cout<<(s+mod)%mod<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return (power(x, y/2)*power(x, y/2))%mod;
    else
        return (x*power(x, y/2)*power(x, y/2))%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           for(i=n-1;i>=0;i--)
	           {
	               if(i>0 && arr[i]==arr[i-1] && s==0)
	                  {
	                      i--;
	                      continue;
	                  }
	                  else
	                  {
	                      if(s>0)
	                      {
	                          s = (s - power(p,arr[i]))%mod;
	                      }
	                      else
	                      s = (s + power(p,arr[i]))%mod;
	                  }
	           }
	           cout<<abs(s)<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               s = (s+mod)%mod;
	             // s = modadd(s,mod-power(p,arr[i]));
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }
	               i--;
	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           for(i=j;i>=0;i--)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	              if(s==0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }
	              /*if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }*/

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               s = abs(s);
	               i--;
	           }
	           cout<<s%mod<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll power(ll x, ll y)
{
    if (y == 0)
        return 1;
    else if (y%2 == 0)
        return (power(x, y/2)*power(x, y/2))%mod;
    else
        return (x*power(x, y/2)*power(x, y/2))%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           for(i=n-1;i>=0;i--)
	           {
	               if(arr[i]==arr[i-1] && s==0)
	                  {
	                      i--;
	                      continue;
	                  }
	                  else
	                  {
	                      if(s>0)
	                      {
	                          s = s - power(p,arr[i]);
	                      }
	                      else
	                      s = s + power(p,arr[i]);
	                  }
	           }
	           cout<<abs(s)<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

 ll modadd(ll a, ll b){
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           for(i=j;i>=0;i--)
	           {
	                 // cout<<s<<endl;
	              s = modadd(s,mod-power(p,arr[i]));
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = abs(s - power(p,arr[i]));
	               s = s%mod;
	             // s = modadd(s,mod-power(p,arr[i]));
	           /*   if(s==0 && i>1)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }*/
	               i--;
	           }
	          // s = s - power(p,arr[0]);
	          // s = (s+mod)%mod;
	           cout<<s%mod<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

 ll modadd(ll a, ll b){
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0){
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           for(i=j;i>=0;i--)
	           {
	              s = modadd(s,mod-power(p,arr[i]));
	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z%mod;
    else
        return ((x*z)%mod)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 )
	          { cout<<(n&1)<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
	   else	if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               if(s<0)
	                s = s+mod;
	               // s = s%mod;
                   if(s==0 && i>0)
                    {
                        i--;
                        s = mod + power(p,arr[i]);
                    }
	               i--;
	           }
	           s = s%mod;
	           s+=mod;
	           s%=mod;
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           for(i=n-1;i>=0;i--)
	           {
	               if(i>0 && arr[i]==arr[i-1] && s==0)
	                  {
	                      i--;
	                      continue;
	                  }
	                  else
	                  {
	                      if(s>0)
	                      {
	                          s = (s - power(p,arr[i]))%mod;
	                      }
	                      else
	                      s = (s + power(p,arr[i]))%mod;
	                  }
	           }
	           cout<<abs(s)<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               if(s<0)
	               s = (s+mod);
	             // s = modadd(s,mod-power(p,arr[i]));
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }
	               i--;
	           }
	           cout<<s%mod<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
                   if(s==0 && i>0)
                    {
                        i--;
                        s = power(p,arr[i]);
                    }
	               i--;
	           }
	           s = s+mod+mod;
	           s = s%mod;
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               s%=mod;

	             // s = modadd(s,mod-power(p,arr[i]));
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }
	               i--;
	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z%mod;
    else
        return ((x*z)%mod)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 )
	          { cout<<(n&1)<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
	   else	if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               if(s<0)
	                s = s+mod;
	               // s = s%mod;
                   if(s==0 && i>0)
                    {
                        i--;
                        s = power(p,arr[i]);
                    }
	               i--;
	           }
	           s = s%mod;
	           s+=mod;
	           s%=mod;
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll asdfgh =  ((a%mod)+(b%mod)+mod)%mod;
   //asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           for(i=j;i>=0;i--)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	              if(s<=0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }
	              /*if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }*/

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z%mod;
    else
        return ((x*z)%mod)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 )
	          { cout<<(n&1)<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
	   else	if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               if(s<0)
	                s = s+mod;
	                s = s%mod;
                   if(s==0 && i>0)
                    {
                        i--;
                        s = mod + power(p,arr[i]);
                    }
	               i--;
	           }
	           s = s%mod;
	           s+=mod;
	           s%=mod;
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           for(i=j;i>=0;i--)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	              if(s<=0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }
	              /*if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }*/

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           while(i>=0)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	            /*  if(s==0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }*/

	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }
	               i--;

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

 ll modadd(ll a, ll b){
   ll asdfgh =  ((a%mod)+(b%mod)+mod+mod)%mod;
   asdfgh = (asdfgh+mod)%mod;
   return asdfgh;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           for(i=j;i>=0;i--)
	           {
	                 // cout<<s<<endl;
	                 if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }
	              s = modadd(s,mod-power(p,arr[i]));
	              c--;
	              if(c==0)
	              {
        				c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }
	              /*if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }*/

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           //cout<<i<<endl;
	           while(i>=0)
	           {
	                 // cout<<s<<endl;
	                 /*if(arr[i]!=arr[i+1])
	                 {
			        	for(j = arr[i];c<1000001 && j<arr[i+1];j++)
				            c = c*p;
		         	  }*/
	              s = modadd(s,mod-power(p,arr[i]));
	              //c--;
	            /*  if(s==0)
	              {
        				//c = 1;
        				i--;
        				if(i==-1)
        				s=0;
        				else
        				s = power(p,arr[i]);
			    }*/
			    i--;
	              if(s==0 && i>0)
	              {
	                  i--;
	                  s = power(p,arr[i]);
	              }

	           }
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z%mod;
    else
        return ((x*z)%mod)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 )
	          { cout<<(n&1)<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
	   else	if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
	               if(s<0)
	                s = s+mod;
	                s = s%mod;
	                if(s==332292606)
	                  break;
                   if(s==0 && i>0)
                    {
                        i--;
                        s = mod + power(p,arr[i]);
                    }
	               i--;
	           }
	           s = s%mod;
	           s+=mod;
	           s%=mod;
	           cout<<s<<endl;
	   }
	return 0;
}


//////////////                AAYUSH SINGH NARUKA                         \\\\\\\\\\\\\\\\\\

//////////////                     AA18                        \\\\\\\\\\\\\\\\\\

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define fi first
#define se second
#define pb push_back
#define front(i,x,n) for( i=x;i<n;i++)
#define back(i,x,n) for( i=x;i>=0;i--)
#define all(x) x.begin(),x.end()
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
#define pr(x) printf("%lld",x)
#define sc(x) scanf("%d", &x)
#define sl(x) scanf("%lld", &x)
#define endl "\n"
#define space printf(" ")
#define jaldi ios_base::sync_with_stdio(false);cin.tie(NULL);
#define test cin>>t;while(t--)

ll modadd(ll a, ll b)
{
   ll ans =  (a%mod+b%mod+mod)%mod;
   return ans;
}

ll power(ll x, ll y)
{
    ll temp;
    if( y == 0)
        return 1;
    temp = (power(x, y/2))%mod;
    ll z = (temp*temp)%mod;
    if (y%2 == 0)
        return z;
    else
        return (x*z)%mod;
}

int main()
{
    jaldi
	ll t,n,i,j,c,s,m,q,k,p;
    test
	   { s=0,c=1;;
	       cin>>n>>p;
	      ll arr[n];
	      for(i=0;i<n;i++)
	        cin>>arr[i];
	        sort(arr,arr+n);
	        if(p==1 && n%2==0)
	          { cout<<0<<endl;
	           continue;}
	        else if(p==1 && n%2==1)
	           { cout<<1<<endl;
	           continue;}
	           i = n-1;
	           while(i>0)
	           {
        			if(arr[i]==arr[i-1])
        			{
        				i = i-2;
        				continue;
        			}
    		    	break;
		        }
		if(i==-1)
		{
			 cout<<0<<endl;
			 continue;
		}
		if(i==0)
		{
			cout<<power(p,arr[0])<<endl;
			continue;
		}
	           s = power(p,arr[i]);
	           i--;
	           j =i;
	           while(i>=0)
	           {
	               s = s - power(p,arr[i]);
                   if(s==0 && i>0)
                    {
                        i--;
                        s = power(p,arr[i]);
                    }
	               i--;
	           }
	           cout<<s%mod<<endl;
	   }
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define N 1000005
long long int n,k,m;
long long int i,p;
int check_v(long long int s, long long int index,long long int n){
    if(s==0){
        return 1;
    }
    // cout<<"In check_v:"<<"s:"<<s<<"index:"<<index<<"n:"<<n<<"\n";
    long long int ans = 1;
    long long int flag=1;
    long long int i=1;
    double v=s;
    // cout<<"s:"<<s<<"index"<<index<<"n:"<<n<<"p:"<<p<<"\n";
    while(i<=index){
        v = (double)v/p;
        if(v<=n){
            return 1;
        }
        i++;
    }
    return 0;
}
long long int expo(long long int a,long long int b){
    long long int x = b;
    long long int ans=1,y=a;
    while(x>0){
        if(x%2==1){
            ans = (ans*y)%MOD;
        }
        y = (y*y)%MOD;
        x = x/2;
    }
    return ans;
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #ifndef ONLINE_JUDGE
    freopen("input1.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int t;
    cin>>t;

    vector<long long int> arr(N);
    // vector<int>freq(N);
    int j,i;
    int g = t;
    while(t--){
        long long int p1 = 0,p2 = 0;
        cin>>n>>p;
        if(g==9999 && t==9999){
            cout<<"n"<<n<<"p"<<p;
            for(i=0;i<n;i++){
                cout<<"i"<<arr[i];
            }
            cout<<"\n";
        }
        for(i=0;i<n;i++){
            cin>>arr[i];
        }
        sort(arr.begin(),arr.begin()+n,greater<int>());
        p1 = arr[0];
        long long int curr_sum = expo(p,arr[0]);
        map<long long int,long long int> m;
        for(i=1;i<n;i++){
            // cout<<"i:"<<i<<"curr_sum:"<<curr_sum<<"\n";
            if(curr_sum==0){
                curr_sum = (curr_sum + expo(p,arr[i]))%MOD;
                p1 = arr[i];
            }
            else{
                curr_sum = (curr_sum - expo(p,arr[i])+MOD)%MOD;
                if(m.find(arr[i])==m.end()){
                    m[arr[i]]=1;
                    if(m[p1]==1){
                        // curr_sum = 0;
                        m.clear();
                    }
                }
                else{
                    m[arr[i]]++;
                    long long int curr = arr[i];
                    while(m[curr]==p){
                        m[curr]=0;
                        if(m.find(curr+1)==m.end()){
                            m[curr+1]=1;
                        }
                        else{
                            m[curr+1]++;
                        }
                        curr++;
                    }
                    if(m[p1]==1){
                        // curr_sum = 0;
                        m.clear();
                        continue;
                    }
                }
            }
            // map<int,int>::iterator itr;
            // for(itr = m.begin();itr!=m.end();itr++){
            //     cout<<"first:"<<itr->first<<"second:"<<itr->second<<"\n";
            // }
        }
        cout<<curr_sum<<"\n";
    }

    return 0;
}