#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <ctype.h>
#define sz(a) (int)a.size()

using namespace std;

typedef long long int ill;
typedef unsigned long long int ull;

int main()
{
	int t;
	cin >> t;

	while (t--)
	{
		int cnt = 0;
		int cnt2 = 0;
		int cnt3 = 0;
		int a, b;
		cin >> a >> b;
		int idx1 = -10, idx2 = -10;

		while (b)
		{
			if (idx1 == -10)
			{
				idx1 = a - 2;
				idx2 = a - 1;
				--b;
			}
			else if (idx1 + 1 == idx2)
			{
				idx1--;
				idx2 = a - 1;
				--b;
			}
			else
			while (idx1 + 1 != idx2)
			{
				idx2--;
				b--;
				if (!b)
					break;
			}

		}
		string q(a, 'a');
		q[idx1] = 'b';
		q[idx2] = 'b';
		cout << q << endl;

	}

}


#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <initializer_list>
#include <tuple>
#include <stack>
#include <numeric>
#include <fstream>
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using ld = long double;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;
		string str(n,'a');
		int s = 2;
		while (k > s - 1)
		{
			k -= s - 1;
			s++;
		}
		str[n - k] = 'b';
		str[n - s] = 'b';
		cout << str << endl;
	}
	return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <initializer_list>
#include <tuple>
#include <stack>
#include <numeric>
#include <fstream>
#define all(v) v.begin(),v.end()
using namespace std;
using ll = long long;
using ld = long double;

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		cin >> n >> k;
		string str(n,'a');
		int s = 1,l = 2,r = 1;
		while (s < k)
		{
			if (l - 1 == r)
			{
				l++;
				r = 1;
				s++;
				continue;
			}
			r++;
			s++;
		}
		str[n - r] = 'b';
		str[n - l] = 'b';
		cout << str << endl;
	}
	return 0;
}


#include <stdio.h>
#include <math.h>

char s[1<<18];

int main()
{
   int t; scanf("%d",&t);
   while(t--){
      int n, k;scanf("%d%d",&n,&k);
      for(int i = 0; i < n; ++i)s[i] = 'a';
      for(int i= 1; i < n; ++i){
         if (k > i) k -= i;
         else {
            s[n-i-1] = 'b';
            break;
         }
      }

      // k > 0 -->
      --k;
      s[n-k-1] = 'b';
      puts(s);
   }
   return 0;
}


#include <stdio.h>
#include <math.h>

char s[1<<18];

int main()
{
   int t; scanf("%d",&t);
   while(t--){
      int n, k;scanf("%d%d",&n,&k);
      for(int i = 0; i < n; ++i)s[i] = 'a';
      for(int i= 1; i < n; ++i){
         if (k > i) k -= i;
         else {
            s[n-i-1] = 'b';
            break;
         }
      }

      // k > 0 -->
      --k;
      s[n-k-1] = 'b';
      s[n] = '\0';
      puts(s);
   }
   return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#define ll long long int
#define MAX 1e8
#define mod 9987
using namespace std;
int main()
{
	int t;
	scanf("%d", &t);
	int n, k;
	while (t--)
	{
		scanf("%d%d", &n, &k);
		int max = n, min = 2;
		while (max - 1 > min)
		{
			int mid = (max + min) / 2;
			if ((mid - 1) * mid/2 < k)
			{
				min = mid;
			}
			else
			{
				max = mid;
			}
		}
		printf("%d %d\n", max, min);
		int num1, num2;
		if (k - (min - 1) * min/2 == 0)
		{
			num1 = min;
			num2 = min-1;
		}
		else if (k - (max - 1) * max / 2 == 0)
		{
			num1 = max;
			num2 = max - 1;
		}
		else if (k - (min - 1) * min / 2 <= min)
		{
			num1 = min + 1;
			num2 = k - (min - 1) * min / 2;
		}
		int i;
		for (i = 0; i < n; i++)
		{
			if (i == n - num1 || i == n - num2)
			{
				printf("b");
			}
			else
				printf("a");
		}
		printf("\n");
	}
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#define ll long long int
#define MAX 1e8
#define mod 9987
using namespace std;
int main()
{
	int t;
	scanf("%d", &t);
	int n, k;
	while (t--)
	{
		scanf("%d%d", &n, &k);
		int max = n, min = 2;
		int i,num1,num2;
		for (i = 2; i <= n; i++)
		{
			if (i * (i - 1)/2 >= k)
			{
				num1 = i;
				break;
			}
		}
		num2 = num1-1 - (i * (i - 1)/2 - k);
		for (i = 0; i < n; i++)
		{
			if (i == n - num1 || i == n - num2)
			{
				printf("b");
			}
			else
				printf("a");
		}
		printf("\n");
	}
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#define ll long long int
#define MAX 1e8
#define mod 9987
using namespace std;
int main()
{
	int t;
	scanf("%d", &t);
	int n, k;
	while (t--)
	{
		scanf("%d%d", &n, &k);
		int max = n, min = 2;
		ll i,num1,num2;
		for (i = 2; i <= n; i++)
		{
			if (i * (i - 1)/2 >= k)
			{
				num1 = i;
				break;
			}
		}
		num2 = num1-1 - (i * (i - 1)/2 - k);
		for (i = 0; i < n; i++)
		{
			if (i == n - num1 || i == n - num2)
			{
				printf("b");
			}
			else
				printf("a");
		}
		printf("\n");
	}
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <string.h>
using namespace std;
#define ll long long
const int N = 100005;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n,k;
        long long x;
        cin >> n >> k;
        char s[N];
        memset(s, 0, sizeof(s));
        for (int i = 0; i < n-2;i++)
        {
                s[i] = 'a';
        }
        for (int i = n - 2; i < n;i++)
            s[i] = 'b';
        for (int i = 0; i < k-1;i++)
        {
            next_permutation(s, s + n);
        }
            cout << s << endl;
    }
    return 0;
}


#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#define ll long long int
#define MAX 1e8
#define mod 9987
using namespace std;
int main()
{
	int t;
	scanf("%d", &t);
	int n, k;
	while (t--)
	{
		scanf("%d%d", &n, &k);
		int max = n, min = 2;
		while (max - 1 > min)
		{
			int mid = (max + min) / 2;
			if ((mid - 1) * mid/2 < k)
			{
				min = mid;
			}
			else
			{
				max = mid;
			}
		}
		int num1, num2;
		if (k - (min - 1) * min/2 == 0)
		{
			num1 = min;
			num2 = min-1;
		}
		else if (k - (max - 1) * max / 2 == 0)
		{
			num1 = max;
			num2 = max - 1;
		}
		else if (k - (min - 1) * min / 2 <= min)
		{
			num1 = min + 1;
			num2 = k - (min - 1) * min / 2;
		}
		int i;
		for (i = 0; i < n; i++)
		{
			if (i == n - num1 || i == n - num2)
			{
				printf("b");
			}
			else
				printf("a");
		}
		printf("\n");
	}
}


#include<stdio.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;

int main() {
    int t,i;
    cin >> t;
    for(i=0;i<t;i++) {
        int n, k;
        cin >> n >> k;
        string s(n, 'a');
        for (int i = n - 2; i >= 0; i--)
		{
            if (k <= (n - i - 1))
			{
                s[i] = 'b';
                s[n - k] = 'b';
                cout << s << endl;
                break;
            }
            k -=n - i - 1;
        }
    }
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
	int i,j,n, t,k;
	int index1, index2;
	char a;
	cin >> t;
	for (i = 0; i < t; i++)
	{
		int ji,zong=0;
		cin >> n>>k;

		for (ji = 1; k >zong ; ji++)zong = (ji + 1)*ji / 2;
		ji -= 1;
		index1 = n - ji;
		index2 = n -(ji-1-(zong-k));
		for (j = 1; j <= n; j++)
		{
			if (j == index1 || j == index2)
				cout << 'b';
			else cout << 'a';
		}
		cout << endl;
	}
	return 0;
}


#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int i,j,n, t,k;
int index1, index2;
char a;
cin >> t;
for (i = 0; i < t; i++)
{
int ji,zong=0;
cin >> n>>k;
for (ji = 1; k >zong ; ji++)zong = (ji + 1)*ji / 2;
ji -= 1;
index1 = n - ji;
index2 = n -(ji-1-(zong-k));
for (j = 1; j <= n; j++)
{
if (j == index1 || j == index2)
cout << 'b';
else cout << 'a';
}
cout << endl;
}
return 0;
}


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1022
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "考试2", "考试2\考试2.vcxproj", "{63738E26-FFBE-4BEB-874D-741282BCA98D}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|x64 = Debug|x64
		Debug|x86 = Debug|x86
		Release|x64 = Release|x64
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Debug|x64.ActiveCfg = Debug|x64
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Debug|x64.Build.0 = Debug|x64
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Debug|x86.ActiveCfg = Debug|Win32
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Debug|x86.Build.0 = Debug|Win32
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Release|x64.ActiveCfg = Release|x64
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Release|x64.Build.0 = Release|x64
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Release|x86.ActiveCfg = Release|Win32
		{63738E26-FFBE-4BEB-874D-741282BCA98D}.Release|x86.Build.0 = Release|Win32
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {A11B6A04-663E-4B2B-8E50-6F2946B9D61B}
	EndGlobalSection
EndGlobal


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 3 * sqrt(k); i++)
		{
			if (i * (i + 1) / 2 <= k)
				pos = i;

		}

		if (pos * (pos + 1) / 2 == k)
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1) / 2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
	}

	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if ((LL)i * (LL)(i + 1) / 2 <= k)
				pos = i;

		}

		//cout << 2*sqrt(k) << endl;
		if (pos * (pos + 1) / 2 == k)
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1) / 2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
	}

	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if ((LL)i * (LL)(i + 1) / 2 <= k)
				pos = i;

		}

		//cout << 2*sqrt(k) << endl;
		if (pos * (pos + 1) / 2 == k)
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			//cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1) / 2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			//cout << s << endl;

		}
	}

	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if ((LL)i * (LL)(i + 1) / 2 <= k)
				pos = i;

		}

		cout << 2*sqrt(k) << endl;
		if (pos * (pos + 1) / 2 == k)
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			//cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1) / 2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			//cout << s << endl;

		}
	}

//	system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		int n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if (i * (i + 1) / 2 <= k)
				pos = i;

		}

		if ((pos * (pos + 1) / 2 == k))
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			int d = k - pos * (pos + 1)/2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}

	}

	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if (i * (i + 1) / 2 <= k)
				pos = i;

		}

		if ((pos * (pos + 1) / 2 == k))
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			int d = k - pos * (pos + 1)/2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}

	}

//	system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (int i = 1; i <= 2 * sqrt(k); i++)
		{
			if (i * (i + 1) / 2 <= k)
				pos = i;

		}

		if ((pos * (pos + 1) / 2 == k))
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1)/2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}

	}

	//system("pause");
	return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<stack>
#include<string>
#define LL long long int
#define module 1000000007
using namespace std;

vector<int> res;
vector<int> a;

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int t;
	cin >> t;
	while (t--)
	{
		LL n, k, c, d, pos = 0;
		string s;
		cin >> n;
		cin >> k;

		for (LL i = 1; i <= 2 * sqrt(k); i++)
		{
			if (i * (i + 1) / 2 <= k)
				pos = i;

		}

		if (pos * (pos + 1) / 2 == k)
		{
			string s;

			for (int i = 0; i < n; i++)
			{
				if (i ==  n - 1 - pos || i == n - pos)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
		else
		{
			LL d = k - pos * (pos + 1) / 2;

			for (int i = 0; i < n; i++)
			{
				if (i == n - 2 - pos || i == n - d)
					s += 'b';
				else
					s += 'a';
			}

			cout << s << endl;

		}
	}

	//system("pause");
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
typedef long long int lli;
const lli mod = 1000000007;
#define __ ios_base::sync_with_stdio(false),cin.tie(NULL);
#define endl '\n'
#define MOD(n) ( ( ((n) % (mod)) + (mod) ) % (mod))

lli mod_pow(lli a,lli b){
    lli x = 1;
    while(b){
        if(b&1)x = (x*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return x;
}
lli isp(lli x ,lli diff,lli p){
    if(p == 1)return x;
    lli nd = 1;
    for(lli i = 0;i<diff;i++){
        nd*=p;
        if(nd>maxn)return -1;
    }
    if(x*nd>maxn)return -1;
    return x*nd;

}
vector<int> fr(maxn);
int main(){__
    lli t,n,p,ki;
    cin>>t;
    while(t--){
        cin>>n>>p;
        vector<int> k(n);
        int mx = -1;
        for(int i = 0;i<n;i++){
            cin>>k[i];
            mx = max(k[i],mx);
        }
        fr.assign(mx+1,0);
        for(int i = 0;i<n;i++)fr[k[i]]++;
        sort(k.rbegin(),k.rend());
        k.erase(unique(k.begin(),k.end()),k.end());
        int i = 0;
        lli ans = 0,need = 1;
        while(i<k.size()){
            if(fr[k[i]]%2== 0){i++;continue;}
            bool pos  = false;
            lli last = k[i];
            for(int j = i+1;j<k.size();j++){
                lli nd = isp(need,last-k[j],p);
                if(nd == -1)break;
                if(nd<= fr[k[j]]){
                    pos = true;
                    fr[k[j]]-=nd;
                    i = j;
                    break;
                }
                else{
                    need = nd-fr[k[j]];
                    last = k[j];
                }
            }
            if(pos == false){
                ans = mod_pow(p,k[i]);
                for(int j = i+1;j<k.size();j++){
                    ans -= (mod_pow(p,k[j])*fr[k[j]])%mod;
                    if(ans<0)ans+=mod;
                }
                break;
            }

        }
        cout<<ans<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
typedef long long int lli;
const lli mod = 1000000007;
#define __ ios_base::sync_with_stdio(false),cin.tie(NULL);
#define endl '\n'
#define MOD(n) ( ( ((n) % (mod)) + (mod) ) % (mod))

lli mod_pow(lli a,lli b){
    lli x = 1;
    while(b){
        if(b&1)x = (x*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return x;
}
lli isp(lli x ,lli diff,lli p){
    if(p == 1)return x;
    lli nd = 1;
    for(lli i = 0;i<diff;i++){
        nd*=p;
        if(nd>maxn)return -1;
    }
    if(x*nd>maxn)return -1;
    return x*nd;

}
vector<int> fr(maxn);
int main(){__
    lli t,n,p,ki;
    cin>>t;
    while(t--){
        cin>>n>>p;
        vector<int> k(n);
        int mx = -1;
        for(int i = 0;i<n;i++){
            cin>>k[i];
            mx = max(k[i],mx);
        }
        fr.assign(mx+1,0);
        for(int i = 0;i<n;i++)fr[k[i]]++;
        sort(k.rbegin(),k.rend());
        k.erase(unique(k.begin(),k.end()),k.end());
        int i = 0;
        lli ans = 0,need = 1;
        while(i<k.size()){
            if(fr[k[i]]%2== 0){i++;continue;}
            bool pos  = false;
            lli last = k[i];
            need = 1;
            for(int j = i+1;j<k.size();j++){
                lli nd = isp(need,last-k[j],p);
                if(nd == -1)break;
                if(nd<= fr[k[j]]){
                    pos = true;
                    fr[k[j]]-=nd;
                    i = j;
                    break;
                }
                else{
                    need = nd-fr[k[j]];
                    last = k[j];
                }
            }
            if(pos == false){
                ans = mod_pow(p,k[i]);
                for(int j = i+1;j<k.size();j++){
                    ans -= (mod_pow(p,k[j])*fr[k[j]])%mod;
                    if(ans<0)ans+=mod;
                }
                break;
            }

        }
        cout<<ans<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
typedef long long int lli;
const lli mod = 1000000007;
#define __ ios_base::sync_with_stdio(false),cin.tie(NULL);
#define endl '\n'
#define MOD(n) ( ( ((n) % (mod)) + (mod) ) % (mod))

lli mod_pow(lli a,lli b){
    lli x = 1;
    while(b){
        if(b&1)x = (x*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return x;
}
lli isp(lli x ,lli diff,lli p){
    if(p == 1)return x;
    lli nd = 1;
    for(lli i = 0;i<diff;i++){
        nd*=p;
        if(nd>maxn)return -1;
    }
    if(x*nd>maxn)return -1;
    return x*nd;

}
vector<int> fr(maxn);
int main(){__
    lli t,n,p,ki;
    cin>>t;
    while(t--){
        cin>>n>>p;
        vector<int> k(n);
        int mx = -1;
        for(int i = 0;i<n;i++){
            cin>>k[i];
            mx = max(k[i],mx);
        }
        fr.assign(mx,0);
        for(int i = 0;i<n;i++)fr[k[i]]++;
        sort(k.rbegin(),k.rend());
        k.erase(unique(k.begin(),k.end()),k.end());
        int i = 0;
        lli ans = 0,need = 1;
        while(i<k.size()){
            if(fr[k[i]]%2== 0){i++;continue;}
            bool pos  = false;
            lli last = k[i];
            for(int j = i+1;j<k.size();j++){
                lli nd = isp(need,last-k[j],p);
                if(nd == -1)break;
                if(nd<= fr[k[j]]){
                    pos = true;
                    fr[k[j]]-=nd;
                    i = j;
                    break;
                }
                else{
                    need = nd-fr[k[j]];
                    last = k[j];
                }
            }
            if(pos == false){
                ans = mod_pow(p,k[i]);
                for(int j = i+1;j<k.size();j++){
                    ans =  (ans-(mod_pow(p,k[j])*fr[k[j]])%mod)%mod;
                    MOD(ans);
                }
                break;
            }

        }
        cout<<ans<<endl;
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
typedef long long int lli;
const lli mod = 1000000007;
#define __ ios_base::sync_with_stdio(false),cin.tie(NULL);
#define endl '\n'
#define MOD(n) ( ( ((n) % (mod)) + (mod) ) % (mod))

lli mod_pow(lli a,lli b){
    lli x = 1;
    while(b){
        if(b&1)x = (x*a)%mod;
        a = (a*a)%mod;
        b>>=1;
    }
    return x;
}
lli isp(lli x ,lli diff,lli p){
    if(p == 1)return x;
    lli nd = 1;
    for(lli i = 0;i<diff;i++){
        nd*=p;
        if(nd>maxn)return -1;
    }
    if(x*nd>maxn)return -1;
    return x*nd;

}
vector<int> fr(maxn);
int main(){__
    lli t,n,p,ki;
    cin>>t;
    while(t--){
        cin>>n>>p;
        vector<int> k(n);
        int mx = -1;
        for(int i = 0;i<n;i++){
            cin>>k[i];
            mx = max(k[i],mx);
        }
        for(int i = 0;i<n;i++)fr[k[i]]++;
        sort(k.rbegin(),k.rend());
        k.erase(unique(k.begin(),k.end()),k.end());
        int i = 0;
        lli ans = 0,need = 1;
        while(i<k.size()){
            if(fr[k[i]]%2== 0){i++;continue;}
            bool pos  = false;
            lli last = k[i];
            need = 1;
            for(int j = i+1;j<k.size();j++){
                lli nd = isp(need,last-k[j],p);
                if(nd == -1)break;
                if(nd<= fr[k[j]]){
                    pos = true;
                    fr[k[j]]-=nd;
                    i = j;
                    break;
                }
                else{
                    need = nd-fr[k[j]];
                    last = k[j];
                }
            }
            if(pos == false){
                ans = mod_pow(p,k[i]);
                for(int j = i+1;j<k.size();j++){
                    ans -= (mod_pow(p,k[j])*fr[k[j]])%mod;
                    if(ans<0)ans+=mod;
                }
                break;
            }

        }
        for(int i = 0;i<k.size();i++)fr[k[i]] = 0;
        cout<<ans<<endl;
    }

    return 0;
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2         1000000007
#define mod1         999998639
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end());
  ll ans1=0;
  ll ans2=0;
  for(ll i=n-1;i>=0;i--)
  {
   if(ans1==0 && ans2==0)
   {
    ans1+=pow_mod(p,v[i],mod1);
    ans2+=pow_mod(p,v[i],mod2);
   }
   else
   {
    ans1=(mod1+ans1-pow_mod(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-pow_mod(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2         1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int  main()
{
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end(),greater<ll>());
  ll ans1=0;
  ll ans2=0;
  for(ll i=0;i<n;i++)
  {
   if(!ans1 && !ans2)
   {
    ans1+=pow_mod(p,v[i],mod1);
    ans2+=pow_mod(p,v[i],mod2);
   }
   else
   {
    ans1=(mod1+ans1-pow_mod(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-pow_mod(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2          1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end());
  ll ans1=0;
  ll ans2=0;
  for(ll i=n-1;i>=0;i--)
  {
   if(ans1==0 && ans2==0)
   {
    ans1=(ans1+modExpo(p,v[i],mod1))%mod1;
    ans2=(ans2+modExpo(p,v[i],mod2))%mod2;
   }
   else
   {
    ans1=(mod1+ans1-modExpo(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-modExpo(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2         1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<int>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end(),greater<int>());
  int ans1=0;
  int ans2=0;
  for(int i=0;i<n;i++)
  {
   if(!ans1 && !ans2)
   {
    ans1+=pow_mod(p,v[i],mod1);
    ans2+=pow_mod(p,v[i],mod2);
   }
   else
   {
    ans1=(mod1+ans1-pow_mod(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-pow_mod(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2         1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end(),greater<ll>());
  ll ans1=0;
  ll ans2=0;
  for(ll i=0;i<n;i++)
  {
   if(!ans1 && !ans2)
   {
    ans1+=pow_mod(p,v[i],mod1);
    ans2+=pow_mod(p,v[i],mod2);
   }
   else
   {
    ans1=(mod1+ans1-pow_mod(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-pow_mod(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2          1000000007
#define mod1         999998639
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end());
  ll ans1=0;
  ll ans2=0;
  for(ll i=n-1;i>=0;i--)
  {
   if(ans1==0 && ans2==0)
   {
    ans1=(ans1+modExpo(p,v[i],mod1))%mod1;
    ans2=(ans2+modExpo(p,v[i],mod2))%mod2;
   }
   else
   {
    ans1=(mod1+ans1-modExpo(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-modExpo(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2         1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int pow_mod(int x , int n , int mod)
{
    int res = 1;
    while(n)
    {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
int  main()
{
 faster;
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end());
  ll ans1=0;
  ll ans2=0;
  for(ll i=n-1;i>=0;i--)
  {
   if(ans1==0 && ans2==0)
   {
    ans1=(ans1+pow_mod(p,v[i],mod1))%mod1;
    ans2=(ans2+pow_mod(p,v[i],mod2))%mod2;
   }
   else
   {
    ans1=(mod1+ans1-pow_mod(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-pow_mod(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


#include<iostream>
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef long long  ll;
typedef unsigned long long int _ll;
#define INF          1LL<<62
#define mod2          1000000007
#define mod1         998244353
#define N            500100
#define PI           3.14159265
#define faster       ios_base::sync_with_stdio(false);cin.tie();cout.tie();
#define pb           push_back
#define ppb          pop_back
#define endl         '\n'
#define mii          unordered_map<ll,ll>
#define mis          unordered_map<ll,string>
#define msi          unordered_map<string,ll>
#define rep(i,a,b)   for(ll i=a;i<b;i++)
#define pii          pair<ll,ll>
#define vi           vector<ll>
#define vs           vector<string>
#define all(a)       (a).begin(),(a).end()
#define F             first
#define S            second
#define PI           3.14159265
 /*ll ls (ll x)
 {
  return ((x)&(-x));
 }*/
 ll pow(ll a ,ll b)
 {
  ll _z=a;
  for(ll i=1;i<b;i++)
    _z=_z*a;
  return _z;
 }
 ll  modExpo( ll x,ll n,ll mod)
{
    if(n==0LL)
        return 1LL;
    else if(n%2LL == 0LL)
    return modExpo((x*x)%mod,n/2LL,mod);
    else
    return (x*modExpo((x*x)%mod,(n-1LL)/2LL,mod))%mod;
}
ll modInverse(ll n)
{
    return modExpo(n,mod2-2LL,mod2);
}
int  main()
{
 int t;
 cin>>t;
 while(t--)
 {
  ll n,p;
  cin>>n>>p;
  vector<ll>v(n);
  for(int i=0;i<n;i++)
  {
   cin>>v[i];
  }
  sort(v.begin(),v.end());
  ll ans1=0;
  ll ans2=0;
  for(ll i=n-1;i>=0;i--)
  {
   if(ans1==0 && ans2==0)
   {
    ans1=(ans1+modExpo(p,v[i],mod1))%mod1;
    ans2=(ans2+modExpo(p,v[i],mod2))%mod2;
   }
   else
   {
    ans1=(mod1+ans1-modExpo(p,v[i],mod1))%mod1;
    ans2=(mod2+ans2-modExpo(p,v[i],mod2))%mod2;
   }
  }
  cout<<ans2<<endl;
 }
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,k;
	ll n,p;
	cin>>n>>p;
	ll a[n];
	rep(i,0,n)cin>>a[i];
	if(p==1){
		cout<<n%2<<endl;
		return;
	}
	sort(a,a+n);
	reverse(a,a+n);
	ll x=0;
	ll ans=0;
	rep(i,0,n){
		if(!x){
			ans=power(p,a[i],M);
			x++;
		}else{
			rep(j,0,a[i-1]-a[i]){
				x*=p;
				if(x>n-1)break;
			}
			if(x>n-1){
				rep(i,i,n){
					ans-=power(p,a[i],M);
					ans+=M;
					ans%=M;
				}
			}else{
				x--;
				ans-=power(p,a[i],M);
				ans+=M;
				ans%=M;
			}
		}
	}
	cout<<ans<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll T=1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k[n];
    map<ll,ll> mp;
    rep(i,0,n)cin>>k[i];
    if(p==1){
        cout<<n%2<<endl;
        return ;
    }
    sort(k,k+n);
    reverse(k,k+n);
    ll x=-1;
    rep(i,0,n){
        if(x==-1){
            x=a[i];
            continue;
        }
        mp[a[i]]++;
        j=a[i];
        while(mp[j]>p-1)mp[j]-=p,j++,mp[j]++;
        if(j==x){
            x=-1;
            mp[j]--;
        }
    }
    if(x==-1){
        cout<<-1<<endl;
        return ;
    }
    x=power(p,x,M);
    for(auto i : mp){
        x-=(power(p,i.F,M)*i.S)%M;
        x+=M;
        x%=M;
    }
    cout<<x<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll a[n];
    map<ll,ll> mp;
    rep(i,0,n)cin>>a[i];
    if(p==1){
        cout<<n%2<<endl;
        return ;
    }
    sort(a,a+n);
    reverse(a,a+n);
    ll x=-1;
    rep(i,0,n){
        if(x==-1){
            x=a[i];
            continue;
        }
        mp[a[i]]++;
        j=a[i];
        while(mp[j]>p-1)mp[j]-=p,j++,mp[j]++;
        if(j==x){
            x=-1;
            mp[j]--;
        }
    }
    if(x==-1){
        cout<<0<<endl;
        return ;
    }
    x=power(p,x,M);
    for(auto i : mp){
        x-=(power(p,i.F,M)*i.S)%M;
        x+=M;
        x%=M;
    }
    cout<<x<<endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k;
    map<ll,ll> mp;
    rep(i,0,n)cin>>k,mp[k]++;
    vpll v;
    if(p==1||!k){
        cout<<n%2<<endl;
        return;
    }
    while(mp.size()){
        auto k = mp.end();
        --k;
        if(k->S%2==0)mp.erase(k);
        else break;
    }
    for(auto k : mp){
        if(k.S)v.pb(k);
    }
    if(!v.size()){
        cout<<0<<endl;
        return;
    }

    ll ans=power(p,v[v.size()-1].F,M);
    ll mx=v[v.size()-1].F-1;
    unordered_map<ll,ll> mp2;
  //  cout<<v.size()<<endl;
      if(v.size()>1)for(i=v.size()-2;i>-1;i--){
        while(v[i].S>p-1&&mx>v[i].F){
            v[i].S-=p;
            mp2[v[i].F+1]++;
            j=v[i].F+1;
            while(j<mx&&mp2[j]>p-1){
                mp2[j]-=p;
                mp2[j+1]++;
                j++;
            }
            while(mp2[mx]>=p-1&&mx>v[i].F)mx--;
        }
       // cout<<i<<" ";
        if(v[i].S){
            if(v[i].S%2){
                mp2[v[i].F]++;
                v[i].S--;
            }
            mp2[v[i].F]=max(mp2[v[i].F],v[i].S+mp2[v[i].F]);
        }
    }
    ll ans2=0;
    for(auto i : mp2){
        ans2+=(power(p,i.F,M)*i.S)%M;
        ans2%=M;
    }
   // cout<<ans<<" "<<ans2<<endl;
    cout<<(ans-ans2+M)%M<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k;
    map<ll,ll> mp;
    rep(i,0,n)cin>>k,mp[k]++;
    vpll v;
    if(p==1||!k){
        cout<<n%2<<endl;
        return;
    }
    while(mp.size()){
        auto k = mp.end();
        --k;
        if(k->S%2==0)mp.erase(k);
        else break;
    }
    for(auto k : mp){
        if(k.S)v.pb(k);
    }
    if(!v.size()){
        cout<<0<<endl;
        return;
    }

    ll ans=power(p,v[v.size()-1].F,M);
    ll mx=v[v.size()-1].F;
    mp.clear();
    repi(i,v.size()-2,-1){
        while(v[i].S>p-1&&mx>v[i].F){
            v[i].S-=p;
            mp[v[i].F+1]++;
            j=v[i].F+1;
            while(j<mx&&mp[j]>p-1){
                mp[j]-=p;
                mp[j+1]++;
                j++;
            }
            while(mp[mx]>=p-1&&mx>v[i].F)mx--;
        }
        if(v[i].S){
            if(v[i].S%2){
                mp[v[i].F]++;
                v[i].S--;
            }
            mp[v[i].F]=max(mp[v[i].F],v[i].S+mp[v[i].F]);
        }
    }
    ll ans2=0;
    for(auto i : mp){
        ans2+=(power(p,i.F,M)*i.S)%M;
    }
   // cout<<ans<<" "<<ans2<<endl;
    cout<<(ans-ans2+M)%M<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k;
    map<ll,ll> mp;
    rep(i,0,n)cin>>k,mp[k]++;
    vpll v;
    if(p==1){
        cout<<n%2<<endl;
        return;
    }
    while(mp.size()){
        auto k = mp.end();
        --k;
        if(k->S%2==0)mp.erase(k);
        else break;
    }
    for(auto k : mp){
        if(k.S>=p){
            mp[k.F+1]+=k.S/p;
            k.S%=p;
        }
        if(k.S)v.pb({k.F,k.S});
    }
    if(!v.size()){
        cout<<0<<endl;
        return;
    }
    //for(auto i :v)cout<<i.F<<" "<<i.S;cout<<endl;
    ll ans=power(p,v[v.size()-1].F,M);
    ans*=v[v.size()-1].S;
    ans%=M;
    rep(i,0,v.size()-1){
        ans-=(power(p,v[i].F,M)*v[i].S)%M;
        ans+=M;
        ans%=M;
    }
    cout<<ans<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,k;
	ll n,p;
	cin>>n>>p;
	ll a[n];
	rep(i,0,n)cin>>a[i];
	if(p==1){
		cout<<n%2<<endl;
		return;
	}
	sort(a,a+n);
	reverse(a,a+n);
	ll x=0;
	ll ans=0;
	rep(i,0,n){
		if(!x){
			ans=power(p,a[i],M);
			x++;
		}else{
			rep(j,0,a[i-1]-a[i]){
				x*=p;
				if(x>n-1)break;
			}
			if(x>n-1){
				rep(i,i,n){
					ans-=power(p,a[i],M);
					ans+=M;
					ans%=M;
				}
			}else{
				x--;
				ans-=power(p,a[i],M);
			}
		}
	}
	cout<<ans<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll T=1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k;
    map<ll,ll> mp;
    rep(i,0,n)cin>>k,mp[k]++;
    vpll v;
    if(p==1||!k){
        cout<<n%2<<endl;
        return;
    }
    while(mp.size()){
        auto k = mp.end();
        --k;
        if(k->S%2==0)mp.erase(k);
        else break;
    }
    for(auto k : mp){
        if(k.S>=p){
            mp[k.F+1]+=k.S/p;
            k.S%=p;
        }
        if(k.S)v.pb({k.F,k.S});
    }
    if(!v.size()){
        cout<<0<<endl;
        return;
    }
    //for(auto i :v)cout<<i.F<<" "<<i.S;cout<<endl;
    ll ans=power(p,v[v.size()-1].F,M);
    ans*=v[v.size()-1].S;
    ans%=M;
    rep(i,0,v.size()-1){
        ans-=(power(p,v[i].F,M)*v[i].S)%M;
        ans+=M;
        ans%=M;
    }
    cout<<ans<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
	ll ans=1;
	while(m){
		if(m%2){
			ans*=a;
			ans%=(mod);
		}
		a=(a*a)%(mod);
		m>>=1;
	}
	return ans;
}

void init(ll* a,ll n,ll val){
	for(ll i=0;i<n;i++)
		a[i]=val;
}

void solve(){
	ll i,j,k;
	ll n,p;
	cin>>n>>p;
	ll a[n];
	rep(i,0,n)cin>>a[i];
	if(p==1){
		cout<<n%2<<endl;
		return;
	}
	sort(a,a+n);
	reverse(a,a+n);
	ll x=0;
	ll ans=0;
	rep(i,0,n){
		if(!x){
			ans=power(p,a[i],M);
			x++;
		}else{
			rep(j,0,a[i-1]-a[i]){
				x*=p;
				if(x>n-1)break;
			}
			if(x>n-1){
				rep(i,i,n){
					ans-=power(p,a[i],M);
					ans+=M;
					ans%=M;
				}
			}else{
				x--;
			}
		}
	}
	cout<<ans<<endl;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll T=1;
	cin>>T;
	while(T--){
		solve();
	}
	return 0;
}


/*input
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define rep(i,j,n) for(i=j;i<n;i++)
#define repi(i,j,n) for(i=j;i>n;i--)
#define inf 100001
#define M 1000000007
#define pie 3.141592653589793238
#define ll long long
#define ld long double
#define vll vector<long long>
#define vi vector<int>
#define pll pair<long long,long long>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define vpll vector<pair<long long,long long> >
#define pb push_back
#define pf pop_front
#define F first
#define S second
#define endl '\n'

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using ordered_set=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

ll power(ll a,ll m,ll mod){
    ll ans=1;
    while(m){
        if(m%2){
            ans*=a;
            ans%=(mod);
        }
        a=(a*a)%(mod);
        m>>=1;
    }
    return ans;
}

void init(ll* a,ll n,ll val){
    for(ll i=0;i<n;i++)
        a[i]=val;
}

void solve(){
    ll i,j;
    ll n,p;
    cin>>n>>p;
    ll k;
    map<ll,ll> mp;
    rep(i,0,n)cin>>k,mp[k]++;
    vpll v;
    if(p==1||!k){
        cout<<n%2<<endl;
        return;
    }
    while(mp.size()){
        auto k = mp.end();
        --k;
        if(k->S%2==0)mp.erase(k);
        else break;
    }
    for(auto k : mp){
        if(k.S)v.pb(k);
    }
    if(!v.size()){
        cout<<0<<endl;
        return;
    }

    ll ans=power(p,v[v.size()-1].F,M);
    ll mx=v[v.size()-1].F;
    mp.clear();
    rep(i,v.size()-2,-1){
        while(v[i].S>p-1&&mx>v[i].F){
            v[i].S-=p;
            mp[v[i].F+1]++;
            j=v[i].F+1;
            while(j<mx&&mp[j]>p-1){
                mp[j]-=p;
                mp[j+1]++;
                j++;
            }
            while(mp[mx]>=p-1&&mx>v[i].F)mx--;
        }
        if(v[i].S){
            if(v[i].S%2){
                mp[v[i].F]++;
                v[i].S--;
            }
            mp[v[i].F]=max(mp[v[i].F],v[i].S+mp[v[i].F]);
        }
    }
    ll ans2=0;
    for(auto i : mp){
        ans2+=(power(p,i.F,M)*i.S)%M;
    }
    cout<<(ans-ans2+M)%M<<endl;

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll T=1;
    cin>>T;
    while(T--){
        solve();
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod1 1000000007
#define mod2 10002
ll k[1000100];
ll quickpow(ll a,ll b,ll mod){
	ll ans=1;
	while(b!=0){
		if(b%2==1){
			ans=(ans%mod*a%mod)%mod;
		}
		a=(a%mod*a%mod)%mod;
		b=b/2;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,p;
		scanf("%lld %lld",&n,&p);
		for(int i=1;i<=n;i++){
			scanf("%lld",&k[i]);
		}
		sort(k+1,k+n+1);
		ll ans=0,res=0;
		for(int i=n;i>0;i--){
			if(ans==0&&res==0){
				ans=quickpow(p,k[i],mod2);
				res=quickpow(p,k[i],mod1);
			}
			else{
				ans=(ans-quickpow(p,k[i],mod2)+mod2)%mod2;
				res=(res-quickpow(p,k[i],mod1)+mod1)%mod1;
			}
		}
		printf("%lld\n",res);
	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod1 1000000007
#define mod2 1000000002
ll k[1000100];
ll quickpow(ll a,ll b,ll mod){
	ll ans=1;
	while(b!=0){
		if(b%2==1){
			ans=(ans%mod*a%mod)%mod;
		}
		a=(a%mod*a%mod)%mod;
		b=b/2;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,p;
		scanf("%lld %lld",&n,&p);
		for(int i=1;i<=n;i++){
			scanf("%lld",&k[i]);
		}
		sort(k+1,k+n+1);
		ll ans=0,res=0;
		for(int i=n;i>0;i--){
			if(ans==0&&res==0){
				ans=quickpow(p,k[i],mod2);
				res=quickpow(p,k[i],mod1);
			}
			else{
				ans=(ans-quickpow(p,k[i],mod2)+mod2)%mod2;
				res=(res-quickpow(p,k[i],mod1)+mod1)%mod1;
			}
		}
		printf("%lld\n",res);
	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod 1000000007
ll k[1000100];
ll quickpow(ll a,ll b){
	ll ans=1;
	while(b!=0){
		if(b%2==1){
			ans=(ans%mod*a%mod)%mod;
		}
		a=(a%mod*a%mod)%mod;
		b=b/2;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,p;
		scanf("%lld %lld",&n,&p);
		for(int i=1;i<=n;i++){
			scanf("%lld",&k[i]);
		}
		sort(k+1,k+n+1);
		ll ans=0,res=0;
		for(int i=n;i>0;i--){
			if(ans==0&&res==0){
				ans=quickpow(p,k[i]);
				res=quickpow(p,k[i]);
			}
			else{
				ans=(ans-quickpow(p,k[i])+mod)%mod;
				res=(res-quickpow(p,k[i])+mod)%mod;
			}
		}
		printf("%lld\n",res);
	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod1 1000000007
#define mod2 10000002
ll k[1000100];
ll quickpow(ll a,ll b,ll mod){
	ll ans=1;
	while(b!=0){
		if(b%2==1){
			ans=(ans%mod*a%mod)%mod;
		}
		a=(a%mod*a%mod)%mod;
		b=b/2;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,p;
		scanf("%lld %lld",&n,&p);
		for(int i=1;i<=n;i++){
			scanf("%lld",&k[i]);
		}
		sort(k+1,k+n+1);
		ll ans=0,res=0;
		for(int i=n;i>0;i--){
			if(ans==0&&res==0){
				ans=quickpow(p,k[i],mod2);
				res=quickpow(p,k[i],mod1);
			}
			else{
				ans=(ans-quickpow(p,k[i],mod2)+mod2)%mod2;
				res=(res-quickpow(p,k[i],mod1)+mod1)%mod1;
			}
		}
		printf("%lld\n",res);
	}
}


#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
#define mod1 1000000007
#define mod2 1000000003
ll k[1000100];
ll quickpow(ll a,ll b,ll mod){
	ll ans=1;
	while(b!=0){
		if(b%2==1){
			ans=(ans%mod*a%mod)%mod;
		}
		a=(a%mod*a%mod)%mod;
		b=b/2;
	}
	return ans;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		ll n,p;
		scanf("%lld %lld",&n,&p);
		for(int i=1;i<=n;i++){
			scanf("%lld",&k[i]);
		}
		sort(k+1,k+n+1);
		ll ans=0,res=0;
		for(int i=n;i>0;i--){
			if(ans==0&&res==0){
				ans=quickpow(p,k[i],mod2);
				res=quickpow(p,k[i],mod1);
			}
			else{
				ans=(ans-quickpow(p,k[i],mod2)+mod2)%mod2;
				res=(res-quickpow(p,k[i],mod1)+mod1)%mod1;
			}
		}
		printf("%lld\n",res);
	}
}


#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;

            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 );

        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;

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

                numbers[ nest[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[ nest[i] ] = 0;
}

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

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

    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 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 != 6 ) return;

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

        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 == 1000 ) 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 ] );

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

    if( numbers[ nest[i] ] ){

        one = ( one - (long long)numbers[ nest[i] ] * power( par , nest[i] ) + 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 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;

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

                numbers[ 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 != 6 ) return;

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

        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 == 1000 ) 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 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;

            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 ) continue;

    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] ) + 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 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];

int mod1 = (int)1e9 + 7 , mod2 = (int)1e6 + 3;

int fast_pow( int num , int times , int mod )
{
    int res = 1;

    while( times ){

        if( times & 1 ) res = (long long)res * num % mod;

        num = (long long)num * num % mod;

        times /= 2;
    }

    return res;
}

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

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

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

    int res = 0, guard_res = 0;

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

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

    if( !res && !guard_res ){

        res = ( res + fast_pow( par , numbers[i] , mod1 ) )%mod1;

        guard_res = ( guard_res + fast_pow( par , numbers[i] , mod2 ) )%mod2;
    }

    else{

        res = ( res - fast_pow( par , numbers[i] , mod1 ) + mod1 )%mod1;

        guard_res = ( guard_res - fast_pow( par , numbers[i] , mod2 ) + mod2 ) %mod2;
    }

    printf("%d\n", res );
}

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

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

    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 ] );

    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;

    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 );

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

    process();

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

        numbers[ nest[i] ] = 0;
}

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

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

    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 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;

            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 );

    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] -= (int)need;

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

                numbers[ nest[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[ nest[i] ] = 0;
}

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

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

    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] ; 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;

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

                numbers[ nest[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[ nest[i] ] = 0;
}

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

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

    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 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;
    }

    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;

            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] ]++;
    }

    if( la && run != 272 ) return;

    if( la && run == 272 ){

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

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

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

        printf("\n");
    }

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

    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];

int moder = (int)1e9 + 7;

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 & 1 ) now = (long long)now * num %moder;

    return now;
}

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

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

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

    int res = 0;

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

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

    if( !res )

        res = ( res + power( par , numbers[i] ) )%moder;

    else

        res = ( res - power( par , numbers[i] ) + moder ) % moder;

    printf("%d\n", res );
}

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

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

    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] -= (int)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[ nest[i] ] = 0;
}

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

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

    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 ] );

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

    if( numbers[ nest[i] ] ){

        one = ( one - (long long)numbers[ nest[i] ] * power( par , nest[i] ) + 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 , numbers[ 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();
}