#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 2e5 + 10;
const int MS = 20;

int suf[MR][MS];

int get(int pos, int ile, int k)
{
	int s = 0;
	while (ile)
	{
		REP(i, MS)
			if ((1 << i) > ile)
			{
				s += suf[pos][i - 1];
				ile -= 1 << (i - 1);
				pos += k * (1 << (i - 1));
				break;
			}
	}

	return s;
}

int main()
{
	int t;
	scanf("%d", &t);
	REP(zz, t)
	{
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		vector<int> V;
		REP(i, n)
		{
			int a;
			scanf("%d", &a);
			V.push_back(a);
		}
		sort(V.begin(), V.end());

		FORD(i, n, 0)
			suf[i][0] = V[i];
		FOR(l, 1, MS)FORD(i, n, 0)
			if (i + ((1 << l) - 1)*(LL)k < n)
				suf[i][l] = suf[i][l - 1] + suf[i + (1 << (l - 1))*k][l - 1];

		int res = 0;

		int lst = n, prv = -1, ile = 0;

		while (lst)
		{
			if (V[lst - 1] > p)
			{
				if (prv != -1)
				{
					p += get(prv - 1, ile, k);
					prv--;
					p -= get(prv - 1, ile, k);
				}

				lst--;
			}
			else
			{
				p -= V[lst - 1];

				if (lst >= k)
				{
					prv = lst;
					ile++;

					res += k;
					lst -= k;
				}
				else
				{
					res++;
					lst--;
				}
			}
		}

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

	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 2e5 + 10;
const int MS = 20;

int suf[MR][MS];

int get(int pos, int ile, int k)
{
	int s = 0;
	while (ile)
	{
		REP(i, MS)
			if ((1 << i) > ile)
			{
				s += suf[pos][i - 1];
				ile -= 1 << (i - 1);
				pos += k * (1 << (i - 1));
				break;
			}
	}

	return s;
}

int main()
{
	int t;
	scanf("%d", &t);
	REP(zz, t)
	{
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		vector<int> V;
		REP(i, n)
		{
			int a;
			scanf("%d", &a);
			V.push_back(a);
		}
		sort(V.begin(), V.end());

		FORD(i, n, 0)
			suf[i][0] = V[i];
		FOR(l, 1, MS)FORD(i, n, 0)
			if (i + ((1 << l) - 1)*(LL)k < n)
				suf[i][l] = suf[i][l - 1] + suf[i + (1 << (l - 1))*k][l - 1];

		int res = 0;

		int lst = n, prv = -1, ile = 0;

		while (lst)
		{
			if (V[lst - 1] > p)
			{
				if (prv != -1)
				{
					p += get(prv - 1, ile, k);
					prv--;
					p -= get(prv - 1, ile, k);
				}

				lst--;
			}
			else if (lst >= k)
			{
				p -= V[lst - 1];

				prv = lst;
				ile++;

				res += k;
				lst -= k;
			}
			else
				break;
		}

		REP(i, lst)
			if (V[i] <= p)
			{
				p -= V[i];
				res++;
			}
			else
			{
				if (prv == -1)
					break;

				while (i < lst - 1)
				{
					p += get(prv - 1, ile, k);
					prv--;
					p -= get(prv - 1, ile, k);
					lst--;

					if (V[i] <= p)
					{
						p -= V[i];
						res++;
						break;
					}
				}
			}

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

	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 2e5 + 10;

pair<int, int> dp[MR];

int main()
{
	int t;
	scanf("%d", &t);
	REP(zz, t)
	{
		int n, p, k;
		vector<int> V;
		scanf("%d%d%d", &n, &p, &k);
		REP(i, n)
		{
			int a;
			scanf("%d", &a);
			V.push_back(a);
		}
		sort(V.begin(), V.end());

		dp[0] = MP(0, p);

		FORQ(i, 1, n)
		{
			dp[i] = dp[i - 1];
			if (i >= k && dp[i - k].second >= V[i - 1])
				dp[i] = max(dp[i], MP(dp[i - k].first + k, dp[i - k].second - V[i - 1]));
			if (dp[i - 1].second >= V[i - 1])
				dp[i] = max(dp[i], MP(dp[i - 1].first + 1, dp[i - 1].second - V[i - 1]));
		}

		printf("%d\n", dp[n].first);
	}

	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 2e5 + 10;
const int MS = 20;

int suf[MR][MS];

int get(int pos, int ile, int k)
{
	int s = 0;
	while (ile)
	{
		REP(i, MS)
			if ((1 << i) > ile)
			{
				s += suf[pos][i - 1];
				ile -= 1 << (i - 1);
				pos += k * (1 << (i - 1));
				break;
			}
	}

	return s;
}

int main()
{
	int t;
	scanf("%d", &t);
	REP(zz, t)
	{
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		vector<int> V;
		REP(i, n)
		{
			int a;
			scanf("%d", &a);
			V.push_back(a);
		}
		sort(V.begin(), V.end());

		FORD(i, n, 0)
			suf[i][0] = V[i];
		FOR(l, 1, MS)FORD(i, n, 0)
			if (i + ((1 << l) - 1)*(LL)k < n)
				suf[i][l] = suf[i][l - 1] + suf[i + (1 << (l - 1))*k][l - 1];

		int res = 0;

		int lst = n, prv = -1, ile = 0;

		while (lst >= k)
		{
			if (V[lst - 1] > p)
			{
				if (prv != -1)
				{
					p += get(prv - 1, ile, k);
					prv--;
					p -= get(prv - 1, ile, k);
				}

				lst--;
			}
			else
			{
				p -= V[lst - 1];

				prv = lst;
				ile++;

				res += k;
				lst -= k;
			}
		}

		REP(i, lst)
			if (V[i] <= p)
			{
				p -= V[i];
				res++;
			}
			else
				break;

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

	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <queue>
#include <bitset>		//UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>		//do setprecision
#include <ctime>
#include <complex>
#include <chrono>
#include<unordered_map>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define ALL(u) (u).begin(),(u).end()

#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double

typedef pair<int, int> PII;

const double pi = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

const int MR = 2e5 + 10;
const int MS = 20;

int suf[MR][MS];

int get(int pos, int ile, int k)
{
	int s = 0;
	while (ile)
	{
		REP(i, MS)
			if ((1 << i) > ile)
			{
				s += suf[pos][i - 1];
				ile -= 1 << (i - 1);
				pos += k * (1 << (i - 1));
				break;
			}
	}

	return s;
}

int main()
{
	int t;
	scanf("%d", &t);
	REP(zz, t)
	{
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		vector<int> V;
		REP(i, n)
		{
			int a;
			scanf("%d", &a);
			V.push_back(a);
		}
		sort(V.begin(), V.end());

		FORD(i, n, 0)
			suf[i][0] = V[i];
		FOR(l, 1, MS)FORD(i, n, 0)
			if (i + ((1 << l) - 1)*(LL)k < n)
				suf[i][l] = suf[i][l - 1] + suf[i + (1 << (l - 1))*k][l - 1];

		int res = 0;

		int lst = n, prv = -1, ile = 0;

		while (lst)
		{
			if (V[lst - 1] > p)
			{
				if (prv != -1)
				{
					p += get(prv - 1, ile, k);
					prv--;
					p -= get(prv - 1, ile, k);
				}

				lst--;
			}
			else if (lst >= k)
			{
				p -= V[lst - 1];

				prv = lst;
				ile++;

				res += k;
				lst -= k;
			}
			else
				break;
		}

		REP(i, lst)
			if (V[i] <= p)
			{
				p -= V[i];
				res++;
			}
			else
				break;

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

	return 0;
}

// FOR GNU C++ use the following pattern:
// Uncomment the code below and change your main into main2
// It does not build in MS C++
// But it does increase the stack size from 256 MB on CF and uses GNU C++

//#include <Processthreadsapi.h>
//#include <iostream>
//#include <Synchapi.h>
//#include <windows.h>
//#include <process.h>
//
//DWORD WINAPI MyThreadFunction(LPVOID lpParam) {
//    main2(nullptr);
//    return 0;
//}
//int main() {
//    auto h = CreateThread(nullptr, 1024 << 20, MyThreadFunction, nullptr, STACK_SIZE_PARAM_IS_A_RESERVATION, nullptr);
//    WaitForSingleObject(h, INFINITE);
//}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <iostream>

using namespace std;

int64_t a[1111111];
int64_t mn[1111111];
int64_t sm[1111111];

int main()
{
    int t;
    scanf("%i", &t);
    while (t--)
    {
        int n, k;
        int64_t p;
        scanf("%i%lld%i", &n, &p, &k);
        for (int i = 0; i < n; i++)
        {
            scanf("%lld", &a[i]);
        }
        sort(a, a + n);
        for (int i = 1; i <= n; i++)
        {
            if (i >= k)
                mn[i] = mn[i - k] + a[i-1];
            else
                mn[i] = 0;
        }
        sm[0] = 0;
        for (int i = 1; i <= n; i++)
        {
            sm[i] = sm[i - 1] + a[i-1];
        }
        int best = 0;
        for (int i = 1; i <= n; i++)
        {
            int64_t p1 = mn[i];
            int j = i % k;
            int64_t p2 = sm[j];
            if (p1+p2 <= p)
                best = i;
        }
        printf("%i\n", best);
    }
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <iostream>

using namespace std;

int64_t a[1111111];
int best[1111111];
int64_t mn[1111111];

int main()
{
    int t;
    scanf("%i", &t);
    while (t--)
    {
        int n, k;
        int64_t p;
        scanf("%i%lld%i", &n, &p, &k);
        for (int i = 0; i < n; i++)
        {
            scanf("%lld", &a[i]);
        }
        sort(a, a + n);
        best[0] = 0;
        mn[0] = p;
        int ans = 0;
        int64_t bp = p;
        for (int i = 0; i < n; i++)
        {
            best[i+1] = ans;
            mn[i+1] = bp;
            if (mn[i] >= a[i])
            {
                best[i+1] = best[i]+1;
                mn[i+1] = mn[i]-a[i];
            }
            if (i-k+1 >= 0 && mn[i-k+1] >= a[i] && best[i-k+1]+k > best[i+1])
            {
                best[i+1] = best[i-k+1]+k;
                mn[i+1] = mn[i]-a[i];
            }
            if (best[i+1] > ans)
            {
                ans = best[i+1];
                bp = mn[i+1];
            }
        }
        printf("%i\n", ans);
    }
}


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdint.h>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <iostream>

using namespace std;

int64_t a[1111111];
int best[1111111];
int64_t mn[1111111];

int main()
{

    int t;
    scanf("%i", &t);
    while (t--)
    {
        int n, k;
        int64_t p;
        scanf("%i%lld%i", &n, &p, &k);
        for (int i = 0; i < n; i++)
        {
            scanf("%lld", &a[i]);
        }
        sort(a, a + n);
        best[0] = 0;
        mn[0] = p;
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            best[i+1] = 0;
            mn[i+1] = 0;
            if (mn[i] >= a[i])
            {
                best[i+1] = best[i]+1;
                mn[i+1] = mn[i]-a[i];
            }
            if (i-k+1 >= 0 && mn[i-k+1] >= a[i] && best[i-k+1]+k > best[i+1])
            {
                best[i+1] = best[i-k+1]+k;
                mn[i+1] = mn[i]-a[i];
            }
            if (best[i+1] > ans)
                ans = best[i+1];
        }
        printf("%i\n", ans);
    }
}


#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
#include <numeric>
#include <deque>
#include <string>
#include <queue>
using namespace std;
const int N = 3e5;
vector<int> a;
vector<int> mods;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int arr = 0;
		int n, p, k; cin >> n >> p >> k;
		a.push_back(0);
		mods.push_back(0);
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			a.push_back(x);
		}
		sort(a.begin(), a.end());
		for (int i = 1; i <= k - 1; i++) {
			mods.push_back(mods[i - 1] + a[i]);
			if (mods[i] <= p) arr = i;
		}
		for (int i = k; i <= n; i++) {
			mods[i % k] += a[i];
			if (mods[i % k] <= p) arr = i;
		}
		a.clear();
		mods.clear();
		cout << arr << '\n';
	}
}


#include <iostream>
#include <set>
#include <map>
#include <algorithm>
#include <vector>
#include <cmath>
#include <numeric>
#include <deque>
#include <string>
#include <queue>
using namespace std;
vector<int> a;
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int t; cin >> t;
	while (t--) {
		int arr = 0;
		int n, p, k; cin >> n >> p >> k;
		for (int i = 0; i < n; i++) {
			int x; cin >> x;
			a.push_back(x);
		}
		int l = 0, r = n + 1;
		sort(a.begin(), a.end());
		while (r - l > 1) {
			int mid = (l + r) / 2;
			int cnt = p;
			int i = mid - 1;
			bool f = 1;
			while (i >= 0) {
				if (a[i] <= cnt) {
					cnt -= a[i];
				}
				else {
					f = 0;
					break;
				}
				if (i >= k - 1) i -= k;
				else i--;
			}
			if (f) l = mid;
			else r = mid;
		}
		a.clear();
		cout << l << '\n';
	}
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <math.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long ll;

int n, p, k;
vector<int> arr;
int dp[200001];

void init() {
	cin >> n >> p >> k;
	arr.clear();

	for (int i = 0; i < n; i++) {
		int num;
		cin >> num;
		arr.push_back(num);
		dp[i + 1] = 0;
	}
	arr.push_back(-1);//to make it 1~n
	sort(arr.begin(), arr.end());
}

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

	for (int t = 0; t < T; t++) {

		init();

		for (int i = 1; i < k; i++ ) {
			dp[i] = arr[i] + dp[i-1];
		}
		for (int i = k; i <= n; i++) {
			dp[i] = dp[i - k] + arr[i];
		}

		int ans = 0;
		for (int i = n; i >= 1; i--) {
			if (dp[i] <= p) {
				ans = i;
				break;
			}
		}
		cout << ans << endl;
	}
}


//#pragma optimization_level 3
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <stack>
#include <iomanip>
#include <cmath>
#include <functional>
#include <random>
#include <unordered_map>
#include <unordered_set>
#include <ctime>
#include <chrono>
#include <climits>
#define mp make_pair
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define pb(x) push_back(x);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define MOD 998244353  //(1000*1000*1000+7)
#define inf1 2e9
#define inf2 2e18

vector<vector<long long> > one(int sz) {
	vector<vector<long long> > ret;
	ret.resize(sz);
	for (int i = 0; i < ret.size(); i++) {
		ret[i].resize(sz);
		for (int j = 0; j < ret.size(); j++) {
			ret[i][j] = (i == j);
		}
	}
	return ret;
}

vector<vector<long long> > mult(vector<vector<long long> > a, vector<vector<long long> > b) {
	vector<vector<long long > > ret;
	ret.resize(a.size());
	for (int i = 0; i < ret.size(); i++) {
		ret[i].resize(a.size());
		for (int j = 0; j < ret.size(); j++) {
			ret[i][j] = 0;
		}
	}
	for (int k = 0; k < ret.size(); k++) {
		for (int i = 0; i < a.size(); i++) {
			for (int j = 0; j < b.size(); j++) {
				ret[i][j] = (ret[i][j] + a[i][k] * b[k][j]);
				ret[i][j] %= MOD;
			}
		}
	}
	return ret;
}

vector<vector<long long> > pw(vector<vector<long long> > a, long long b) {
	if (b == 0)
		return one((int)a.size());
	if (b % 2)
		return mult(a, pw(a, b - 1));
	return pw(mult(a, a), b / 2);
}

inline int mul(int a, int b) {
	return int(a * 1ll * b % MOD);
}

inline int norm(int a) {
	if (a >= MOD)
		a -= MOD;
	if (a < 0)
		a += MOD;
	return a;
}

inline int binPow(int a, int k) {
	int ans = 1;
	while (k > 0) {
		if (k & 1)
			ans = mul(ans, a);
		a = mul(a, a);
		k >>= 1;
	}
	return ans;
}

inline int subtract(int a, int b) {
	return norm(a - b);
}

inline int sum(int a, int b) {
	return int((a + b) % MOD);
}

inline int inv(int a) {
	return binPow(a, MOD - 2);
}

ll ceil_int(ll a, ll b) {
	return a / b + (a % b != 0);
}

ll gcd(ll a, ll b) {
	if (a == 0)return b;
	return gcd(b % a, a);
}

ll lcm(ll a, ll b) {
	return (a * b) / gcd(a, b);
}

int getbit(int mask, int bit) {
	return (mask >> bit) & 1;
}

void setbit(int& mask, int bit) {
	mask = (mask | (1 << bit));
}

void offbit(int& mask, int bit) {
	mask = mask & (~(1 << bit));
}

int count(int X)
{
	X = X - ((X >> 1) & 0x55555555);
	X = (X & 0x33333333) + ((X >> 2) & 0x33333333);
	X = (X + (X >> 4)) & 0x0F0F0F0F;
	X = X + (X >> 8);
	X = X + (X >> 16);
	return (X & 0x0000003F);
}

int main() {
	srand(chrono::system_clock::now().time_since_epoch().count());
	cin.sync_with_stdio(false);
	cin.tie(false);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		int n, p, k;
		cin >> n >> p >> k;
		vector<int>a(n);
		vector<int>pref(n + 1);
		for (int j = 0; j < n; j++) {
			cin >> a[j];
		}

		sort(all(a));
		for (int j = 0; j < n; j++) {
			pref[j + 1] = pref[j] + a[j];
		}
		int ans = -1;

		int cp = 0;
		int cur_ans = 0;
		for (int j = 0; j < k; j++) {
			cp = pref[j];
			if (cp > p)break;
			cur_ans = j;

			for (int q = j + k - 1; q < n; q+=k) {
				if (cp + a[q] <= p) {
					cp += a[q];
					cur_ans += k;
				}
				else {
					break;
				}
			}
			ans = max(ans, cur_ans);
			cur_ans = 0;
			cp = 0;
		}
		cout <<  ans << endl;
	}

	return 0;
}


//1282B
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
long long n, p, k;
int t, a[200001];
int main ()
{
  std::cin >> t;
  while (t--)
  {
    std::cin >> n >> p >> k;
    for (int i = 1; i <= n; ++i)
      std::cin >> a[i];
    std::sort (a, a + n + 1);
    int m = 0;
    long long s0 = 0, s1 = 0;
    for (int i = 0; i < k && i <= n && s0 <= p; ++i)
    {
      s0 += a[i];
      s1 = s0;
      if (s1 <= p) m = std::max (m, i);
      for (int j = i + k; j <= n && s1 <= p; j += k)
      {
        s1 += a[j];
        if (s1 <= p) m = std::max (j, m);
      }
    }
    std::cout << m << "\n";
  }
}


#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 2e5 + 10;
int a[maxn];
int dp[maxn];
int main()
{
	int t;
	int n, p, k;
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d %d", &n, &p, &k);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
		}
		sort(a + 1, a + 1 + n);
		for (int i = 0; i <= n; i++) {
			dp[i] = a[i] + dp[i - 1];
			if (i - k >= 0) {
				dp[i] = min(dp[i], a[i] + dp[i - k]);
			}
		}
		int numi = 0;
		for (int i = 0; i <= n; i++)
		{
			if (p >= dp[i]) numi = i;
		}
		printf("%d\n", numi);
	}
	return 0;
}


#include<iostream>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<ctime>
#include<math.h>
#include<fstream>
#include<stdio.h>
#include<stack>
#include<limits.h>
#include<iomanip>  // cout << setprecision(...) <<  fixed;
using namespace std;

#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vs vector<string>
#define pii pair<int, int>
#define pllll pair<ll, ll>
#define psll pair<string, ll>
#define qi queue<int>
#define qll queue<ll>
#define si set<int>
#define sll set<ll>
#define mulsi multiset<int>
#define mulsll multiset<ll>
#define mulss multiset<string>
#define msi map<string, int>
#define mii map<int, int>
#define ci const int
#define cll const ll
#define cld const long double
#define msii multiset<int> :: iterator
#define mslli multiset<ll> :: iterator
#define sii set<int> :: iterator
#define slli set<ll> :: iterator
#define fi first
#define se second
#define pb push_back

ll gcd(ll a, ll b) {
	while (a != b) {
		if (b > a) {

			b -= a;
		}
		else {
			a -= b;
		}
	}
	return a;
}

ll n = 0, m = 0, t = 0, otv = 0, test = 0;
const ll maxn = 4 * 1e5 + 3;
ll d[maxn];
ll v[maxn];
ll b[maxn];
multiset<ll> st;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> t;
	ll a, b, c;
	for (int i = 0; i < t; i++) {
		cin >> a >> b >> c;
		otv = 0;
		for (int j = 1; j <= a; j++) {
			cin >> d[j];
		}
		sort(d + 1, d + a + 1);
		v[1] = d[1];
		for (int j = 2; j <= a; j++) {
			if (j >= c) {
				v[j] = min(v[j - c] + d[j], v[j - 1] + d[j]);
			}
			else {
				v[j] = v[j - 1] + d[j];
			}
		}
		for (int j = 1; j <= a; j++) {
			if (v[j] <= b) {
				otv = j;
			}
			v[j] = 0;
			d[j] = 0;
		}
		cout << otv << endl;
	}
//	system("pause");
}


#include <iostream>
#include <sstream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <algorithm>
using namespace std;

int main()
{
	int t, n, p, k, ans;
	cin >> t;

	while (t--) {
		cin >> n >> p >> k;
		vector<int> a(n), price(n);

		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a.begin(), a.end());

		ans = 0;
		for (int i = 0; i < n; i++) {
			if (i < k)
				price[i] = a[i];
			else
				price[i] = a[i] + price[i - k];

			if (price[i] > p)
				break;
			ans++;
		}

		cout << ans << endl;
	}

	return 0;
}


#include <iostream>
#include <sstream>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <algorithm>
using namespace std;

int main()
{
	int t, n, p, k, ans;
	cin >> t;

	while (t--) {
		cin >> n >> p >> k;
		vector<int> a(n), price(n + 1, 0);

		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a.begin(), a.end());

		for (int i = 1; i <= n; i++) {
			if (i < k)
				price[i] = a[i - 1] + price[i - 1];
			else if (i == k)
				price[i] = a[i - 1];
			else
				price[i] = a[i - 1] + price[i - k];
		}
		ans = 0;
		for (int i = 1; i <= n; i++) {
			if (price[i] <= p)
				ans = i;
		}

		cout << ans << endl;
	}

	return 0;
}


#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <list>

using namespace std;
using ll = long long;

int main()
{
	int tt;
	cin >> tt;
	for (int ttt = 0; ttt < tt; ttt++)
	{

		int n, p, k;
		cin >> n >> p >> k;
		vector<int> coins(n);
		for (int i = 0; i < n; i++)
		{
			cin >> coins[i];
		}
		sort(coins.begin(), coins.end());
		int m = 0;
		int prefSum = 0;
		for (int i = 0; i < k; i++)
		{
			int thisRes = i;
			int ths = prefSum;
			if (prefSum > p)
			{
				break;
			}
			for (int j = i + k - 1; j < n; j+= k)
			{
				ths += coins[j];
				if (ths <= p)
				{
					thisRes += k;
				}
				else
				{
					break;
				}
			}
			prefSum += coins[i];
			m = max(m, thisRes);

		}
		cout << m<<endl;
	}

	return 0;
}


#include<cstdio>
#include<vector>
#include<algorithm>
#include<iostream>
using namespace std;
int main(void) {
	ios::sync_with_stdio(false);
	int t;
	cin >> t;
	while (t--) {
		int n, p, k;
		cin >> n >> p >> k;
		int pref = 0;
		int ans = 0;
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a.begin(), a.end());
		for (int i = 0; i <= k; i++) {
			int sum = pref;
			if (sum > p) break;
			int cnt = i;
			for (int j = i + k - 1; j < n; j += k) {
				if (sum + a[j] <= p) {
					cnt += k;
					sum += a[j];
				}
				else {
					break;
				}
			}
			pref += a[i];
			ans = max(ans, cnt);
		}
		cout << ans << "\n";
	}
	return 0;
}


#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <functional>
#include <stack>
#include <queue>
#include <cstdlib>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;

int n, p, k;
int arr[200001];
int dp[200001];

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> p >> k;

		int cnt = 0;
		for (int i = 1; i <= n; i++)
			cin >> arr[i];
		sort(arr + 1, arr + 1 + n);

		for (int i = 1; i < k; i++) {
			dp[i] = dp[i - 1] + arr[i];
			if (dp[i] <= p)
				cnt = max(cnt, i);
		}
		for (int i = k; i <= n; i++) {
			dp[i] = dp[i - k] + arr[i];
			if (dp[i] <= p)
				cnt = max(cnt, i);
		}
		cout << cnt << "\n";
	}
}


#include <iostream>
#include <string>
#include <iterator>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define sp system("pause");
#define MOD 998244353
#define mxN 200005

void solve()
{
	ll n, p, k;
	int a[mxN];

	cin >> n >> p >> k;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(a, a + n);
	ll ans = 0;
	ll dp[mxN];
	dp[0] = a[0];
	ans = dp[0] <= p ? 1: 0;
	for (ll i = 1; i < n; i++)
	{
		if (i == k - 1)
			dp[i] = a[i];
		else if (i < k)
			dp[i] = a[i] + dp[i - 1];
		else
			dp[i] = a[i] + dp[i - k];
		ans = dp[i] <= p ? max(ans, i + 1) : ans;
	}

	cout << ans << endl;

}
int main()
{
    fast
	int t;

	cin >> t;

	while (t--)
	{
		solve();
	}

	return 0;
}


#include <iostream>
#include <string>
#include <iterator>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <sstream>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define fast  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define ll long long
#define sp system("pause");
#define MOD 998244353
#define mxN 200005
int j = 0;
void solve()
{
	ll n, p, k;
	int a[mxN];

	cin >> n >> p >> k;

	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}

	sort(a, a + n);
	ll ans = 0;
	ll dp[mxN];
	dp[0] = a[0];
	ans = dp[0] <= p ? 1: 0;
	for (ll i = 1; i < n; i++)
	{
		if (i == k - 1)
			dp[i] = a[i];
		else if (i < k)
			dp[i] = a[i] + dp[i - 1];
		else
			dp[i] = a[i] + dp[i - k];
		ans = dp[i] <= p ? max(ans, i + 1) : ans;
	}

	cout << ans << endl;

}
int main()
{
	int t;

	cin >> t;

	while (t--)
	{
		solve();
	}

	return 0;
}


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

#define ll long long
#define pb push_back
int a[200005],d[200005];
int main(){
	int t;
	cin >> t;
	while(t--)
	{
		int n,p,k;
		cin >> n >>p >> k;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			if(i<k) d[i]=d[i-1]+a[i];
			else d[i]=d[i-k]+a[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(d[i]<=p) ans=max(ans,i);
		cout<<ans<<"\n";
	}
}


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

int main()
{
	int t;
	cin >> t;
	while (t--) {
		int n, p, k;
		cin >> n >> p >> k;
		int* a = new int[n];
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a, a + n);
		int l = -1, r = n; //ответом будет первые x подарков котороые мы можем купить
		int c = p;
		while (r > l + 1) {
			c = p;
			int m = (r + l) / 2;
			int point = m;
			while (point > k - 2 && c >= 0) {
				c -= a[point];
				point -= 2;
			}
			while (point >= 0) {
				c -= a[point];
				point--;
			}
			if (c >= 0) l = m;
			else r = m;
		}
		cout << l + 1 << "\n";
	}

}


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

int main()
{
	int t;
	cin >> t;
	while (t--) {
		int n, p, k;
		cin >> n >> p >> k;
		int* a = new int[n];
		for (int i = 0; i < n; i++) cin >> a[i];
		sort(a, a + n);
		int* z = new int[k]; //максимальное количество покупок если мы раскупаем начиная с i-й акции, если 0 то никак
		int* s = new int[k];
		for (int i = 0; i < k; i++) {
			z[i] = 0;
		}
		s[0] = a[0];
		for (int i = 1; i < k; i++) {
			s[i] = s[i - 1] + a[i];
		}
		for (int i = 0; i <= k - 1; i++) {
			int bal = p;
			if (i != 0) {
				bal -= s[i - 1];
			}
			int kucha = 0; //количество куч по k, купленных по акции
			if (bal >= 0) {
				z[i] += i;
				while (bal >= 0 && i + k - 1 + kucha*k < n) {
					bal -= a[i + k - 1 + kucha * k];
					if(bal >= 0) kucha++;
				}
				z[i] += k * kucha;
			}
		}
		int maxindex = 0;
		for (int i = 0; i <= k - 1; i++) {
			if (z[maxindex] < z[i]) maxindex = i;
		}
		cout << z[maxindex] << "\n";
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define repd(x, r, l) for(int x = r; x >= l; x--)
#define clr(x, y) memset(x, y, sizeof(x))
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define MAXN 200005
#define fi first
#define se second
#define SZ(x) ((int)x.size())
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int INF = 1 << 30;
const int p = 1000000009;
int lowbit(int x){ return x & (-x);}
int fast_power(int a, int b){ int x; for(x = 1; b; b >>= 1){ if(b & 1) x = 1ll * x * a % p; a = 1ll * a * a % p;} return x % p;}

int a[MAXN], dp[MAXN];

int main(){
    int t;
    scanf("%d", &t);
    rep(times, 1, t){
        int n, p, k;
        scanf("%d%d%d", &n, &p, &k);
        rep(i, 1, n) scanf("%d", &a[i]);
        sort(a + 1, a + n + 1);
        int ans = 0;
        rep(i, 1, n){
            if(i < k) dp[i] = dp[i - 1] + a[i];
            else dp[i] = dp[i - k] + a[i];
        }
        rep(i, 1, n)
            if(dp[i] <= p) ans = i;
        printf("%d\n", ans);
    }
    return 0;
}


#include <iostream>
#include <set>
#include <iterator>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	int t, n, p, k;
	cin >> t;
	while (t--)
	{
		int ans = 0;
		cin >> n >> p >> k;
		vector<int>A(n + 1, 0), B(n + 1, 0);
		for (int i = 1; i <= n; i++)
			cin >> A[i];
		sort(A.begin(), A.end());
		for (int i = 1; i <= n; i++)
		{
			if (i < k)
				B[i] = B[i - 1] + A[i];
			else
				B[i] = B[i - k] + A[i];
		}
		for (int i = 1; i <= n; i++)
			if (B[i] <= p)
				ans = max(ans, i);
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define ll long long
#define pb push_back
int a[200005],d[200005];
int main(){
	int t;
	cin >> t;
	while(t--)
	{
		int n,p,k;
		cin >> n >>p >> k;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			if(i<k) d[i]=d[i-1]+a[i];
			else d[i]=d[i-k]+a[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(d[i]<=p) ans=max(ans,i);
		cout<<ans<<"\n";
	}
}


#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll list[201001];
int vis[10111];
int main(){
	int t;
	scanf("%d",&t);

	while(t--){
		int n,p,k;
		scanf("%d %d %d",&n,&p,&k);
		for(int i=1;i<=n;i++){
			scanf("%lld",&list[i]);
			vis[list[i]]++;
		}
		int len = 0;
		for(int i=0;i<10100;i++){
			while(vis[i]){
				list[++len] = i;
				vis[i]--;
			}
		}
		ll ans = 0;
		ll cnt = 0;

		int c = p;

		for(int i=1;i<=n;i+=k){
			if(c - list[i] < 0) break;
			if(i == 1) ans++;
			else ans += k;
			c -= list[i];
		}
		len = (n-1) % k;
		for(int i = n - len + 1;i<=n;i++){
			if(c - list[i] < 0) break;
			ans++;
			c -= list[i];
		}
		for(int i=k;i<=n;i+=k){
			if(p - list[i] < 0) break;
			cnt += k;
			p -= list[i];
		}
		len = n % k;
		for(int i = n - len + 1;i<=n;i++){
			if(p - list[i] < 0) break;
			cnt++;
			p -= list[i];
		}
		ans = max(ans,cnt);
		printf("%lld\n",ans);
		cout<<endl;
	}
	return 0;
}


#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
ll list[201001];
int vis[10111];
int main(){
	int t;
	scanf("%d",&t);

	while(t--){
		int n,p,k;
		scanf("%d %d %d",&n,&p,&k);
		for(int i=1;i<=n;i++){
			scanf("%lld",&list[i]);
			vis[list[i]]++;
		}
		int len = 0;
		for(int i=0;i<10100;i++){
			while(vis[i]){
				list[++len] = i;
				vis[i]--;
			}
		}
		ll ans = 0;
		ll cnt = 0;

		int c = p;

		for(int i=1;i<=n;i+=k){
			if(c - list[i] < 0) break;
			if(i == 1) ans++;
			else ans += k;
			c -= list[i];
		}
		len = n % k;
		for(int i = n - len + 1;i<=n;i++){
			if(c - list[i] < 0) break;
			ans++;
			c -= list[i];
		}
		for(int i=k;i<=n;i+=k){
			if(p - list[i] < 0) break;
			cnt += k;
			p -= list[i];
		}
		len = n % k;
		for(int i = n - len + 1;i<=n;i++){
			if(p - list[i] < 0) break;
			cnt++;
			p -= list[i];
		}
		ans = max(ans,cnt);
		printf("%lld\n",ans);

	}
	return 0;
}


#include<iostream>
#include<cstring>
using namespace std;

#define ll long long
#define pb push_back
int a[200005],d[200005];
int main(){
	int t;
	cin >> t;
	while(t--)
	{
		int n,p,k;
		cin >> n >>p >> k;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			if(i<k) d[i]=d[i-1]+a[i];
			else d[i]=d[i-k]+a[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(d[i]<=p) ans=max(ans,i);
		cout<<ans<<"\n";
	}
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

// B1. K for the Price of One (Easy Version)

// https://codeforces.com/contest/1282/problem/B1

typedef int Int;

vector<Int> a;

// Greedy
// * systematic buy of a chunk of k items at price of the last item
// * start of 1st chunk range from 0 to k-1
// * buy all items before the first chunk without promotion

void solve(Int n, Int p, Int k)
{
	Int res = 0;
	sort(a.begin(), a.end());
	Int cumsum = 0; // sum a[0:i], i excluded
 	for (Int i = 0; i < k; i++)
	{
		Int sum = cumsum;
		if (sum > p)
			break;
		Int cnt = i; // buy the first i items
		for (Int j = i + k - 1; j < n; j += k) // j : ends of interval of size k : i, ..., i + k - 1
		{
			if (sum + a[j] <= p) // check if we can buy the k items of the interval
			{
				cnt += k;
				sum += a[j];
			}
			else
			{
				break;
			}
		}
		cumsum += a[i];
		res = max(res, cnt);
    }
    cout << res << '\n';
}

int main(int argc, char const *argv[])
{
	bool vsmode = false;
	if (argc >= 2)
	{
		const string s = argv[1];
		vsmode = (s=="VSCODE");
    }
	std::ifstream in("inputB.txt");
	std::ofstream out("outB.txt");
	if (vsmode)
	{
		std::streambuf *cinbuf = std::cin.rdbuf(); //save old buf
		std::cin.rdbuf(in.rdbuf()); //redirect std::cin to input.txt!

		std::streambuf *coutbuf = std::cout.rdbuf(); //save old buf
		std::cout.rdbuf(out.rdbuf()); //redirect std::cout to out.txt!
	}

	int t;
	cin >> t;
	while (t--)
	{
		Int n, p, k;
	    cin >> n >> p >> k;
		a = vector<Int>(n);
		for (Int i=0; i<n; i++)
		{
			cin >> a[i];
		}
		solve(n, p, k);
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

int a[200000];
int sum[200000];
int main() {
	int t;
	scanf("%d", &t);
	while (t--) {
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		for (int i = 0; i < n; i++) scanf("%d", &a[i]);
		sort(a, a + n);
		sum[0] = a[0];
		for (int i = 1; i < k; i++) {
			sum[i] = sum[i - 1] + a[i];
		}
		int ans = 0;
		int tempP = p;

		for (int i = 0; i < k; i++) {
			int tempAns = 0;
			if (i >= 1) {
				if (sum[i - 1] <= p) {
					tempAns += i;
					p -= sum[i - 1];
				}
			}
			for (int j = i + k - 1; j < n; j += k) {
				if (a[j] <= p) {
					//printf("test: %d %d\n", a[j], p);
					tempAns += k;
					p -= a[j];
				}
			}
			ans = max(tempAns, ans);
			p = tempP;
		}
		printf("%d\n", ans);
	}
}


#include <iostream>
#include <fstream>
#include <map>
#include <unordered_map>
#include <set>
#include <string>
#include <algorithm>
#include <sstream>
#include <assert.h>
#include <bitset>
#include <iomanip>
#include <vector>

#define en "\n"
#define for0(i, n) for(int i = 0; i < n; i++)
#define all(x) (x).begin(), (x).end()
#pragma warning(disable : 4996)
typedef long long ll;
typedef long double ld;

using namespace std;

const int NMAX = 1e5 + 17;

//void solve() {
//	int a, b, c, r;
//	cin >> a >> b >> c >> r;
//	if (a > b)
//		swap(a, b);
//	int len = b - a;
//	a = max(a, c - r);
//	b = min(b, c + r);
//	if (b < a)
//		cout << len << en;
//	else
//		cout << len - (b - a) << en;
//}
//
//
//void main() {
//	ios::sync_with_stdio(false);
//	cin.tie(0);
//	cout.tie(0);
//#ifdef _DEBUG
//	freopen("in.txt", "r", stdin);
//	freopen("out.txt", "w", stdout);
//#endif
//	int q;
//	cin >> q;
//	while(q--)
//		solve();
//}

void solve() {
	int n, p, k;
	cin >> n >> p >> k;
	vector<int> a(n);
	for0(i, n) {
		cin >> a[i];
	}
	sort(all(a));
	ll ans = 0;
	ll pref = 0;
	for (int i = 0; i < k; i++) {
		ll pay = pref;
		if (pay > p)
			break;
		ll kol = i;
		for (int j = i + k - 1; j < n; j += k) {
			if (a[j] + pay > p) {
				break;
			}
			else {
				kol+=k;
				pay += a[j];
			}
		}
		ans = max(ans, kol);
		pref += a[i];
	}
	cout << ans << en;
}

void main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
#ifdef _DEBUG
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int q;
	cin >> q;
	while(q--)
		solve();
}


#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn = 2e5 + 10;
ll a[maxn];
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int t; cin >> t;
	while (t--) {
		ll n, p, k;
		cin >> n >> p >> k;
		for (int i = 0; i < n; i++)
			cin >> a[i];
		sort(a, a + n);
		int sum = 0, res = 0;;
		for (int i = 0; i < k; i++) {
			int s = sum;
			int num = i;
			for (int j = i + k - 1; j < n; j += k) {
				if (s + a[j] <= p) {
					s += a[j];
					num += k;
				}
				if (s + a[j] > p)
					break;
			}
			res = max(num, res);
			sum += a[i];
			if (sum > p)
				break;
		}
		cout << res << endl;
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <iomanip>
#include <cstring>
#include <string.h>
#include <iterator>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <limits.h>
#include <string>
#include <bitset>
#include <map>
#include <numeric>
#include<unordered_map>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define endl cout<<"\n"
#define lli long long int
#define ld long double
#define M_PI (3.14159265358979323846264338327950288)
#define MOD (1000000007)
#define pii pair<lli,lli>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
//using namespace __gnu_pbds;

//fast-exponentiation-lli
unsigned lli expo_fast(lli a, lli b) {
	a = a;
	lli result = 1;
	while (b) {
		//multiplyint(tos(result), a);
		if (b & 1)result = (result * a);
		b >>= 1;
		a = (a*a);
	}
	return (result);
}

void take_in(vector<lli> *arr) {
	for (int i = 0; i < arr->size(); i++)cin >> (*(arr))[i];
}

lli gcd(lli a, lli b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);

}

/* Iterative Function to calculate (x^y)%p in O(log y) */
unsigned lli power(lli x, unsigned lli y, lli p)
{
	lli res = 1;      // Initialize result
	x = x % p;  // Update x if it is more than or equal to p
	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)res = (res*x) % p;
		// y must be even now
		y = y >> 1;
		x = (x*x) % p;
	}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t; cin >> t;
	while (t--) {
		lli n, p, k; cin >> n >> p >> k;
		vector<lli>arr(n); take_in(&arr); sort(arr.begin(), arr.end());
		int lb = 0, ub = n; lli ans = -1;
		while (lb <= ub) {
			int mid = (lb + ub) / 2;
			lli tmp = p; lli cnt = 0;
			for (int i = mid - 1; i > -1; i--) {
				if (!cnt) {
					tmp -= (arr[i]); cnt = k;
				}
				cnt--;
			}
			if (tmp >= 0) {
				ans = mid; lb = mid + 1;
			}
			else ub = mid - 1;
		}
		cout << ans; endl;
	}
	//system("PAUSE");
}


#include <iostream>
#include <vector>
#include <iomanip>
#include <cstring>
#include <string.h>
#include <iterator>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#include <tuple>
#include <limits.h>
#include <string>
#include <bitset>
#include <map>
#include <numeric>
#include<unordered_map>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define endl cout<<"\n"
#define lli long long int
#define ld long double
#define M_PI (3.14159265358979323846264338327950288)
#define MOD (1000000007)
#define pii pair<lli,lli>
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
//using namespace __gnu_pbds;

//fast-exponentiation-lli
unsigned lli expo_fast(lli a, lli b) {
	a = a;
	lli result = 1;
	while (b) {
		//multiplyint(tos(result), a);
		if (b & 1)result = (result * a);
		b >>= 1;
		a = (a*a);
	}
	return (result);
}

void take_in(vector<lli> *arr) {
	for (int i = 0; i < arr->size(); i++)cin >> (*(arr))[i];
}

lli gcd(lli a, lli b)
{
	if (b == 0)
		return a;
	return gcd(b, a % b);

}

/* Iterative Function to calculate (x^y)%p in O(log y) */
unsigned lli power(lli x, unsigned lli y, lli p)
{
	lli res = 1;      // Initialize result
	x = x % p;  // Update x if it is more than or equal to p
	while (y > 0) {
		// If y is odd, multiply x with result
		if (y & 1)res = (res*x) % p;
		// y must be even now
		y = y >> 1;
		x = (x*x) % p;
	}
	return res;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int t; cin >> t;
	while (t--) {
		lli n, p, k; cin >> n >> p >> k;
		vector<lli>arr(n),bono; take_in(&arr); sort(arr.begin(), arr.end());
		int lb = 0, ub = n; lli ans = -1, cost = 0, indx = 0;
		for (int i = 0; i < (n / k) + 1; i++) {
			if (i*k <= n)bono.push_back(i*k);
		}

		ub = bono.size() - 1;
		while (lb <= ub) {
			int mid1 = (lb + ub) / 2,mid = bono[mid1];
			lli tmp = p; lli cnt = 0;
			int i = mid - 1;
			while (i > -1) {
				tmp -= arr[i];
				if (i + 1 >= k) {
					i -= k;
				}
				else {
					i--;
				}
			}
			if (tmp >= 0) {
				ans = mid;
				cost = tmp;
				indx = mid1;
				lb = mid1 + 1;
			}
			else ub = mid1 - 1;
		}
		bono.push_back(n);
		lb = ans, ub = bono[indx + 1];
		while (lb <= ub) {
			int mid = (lb + ub) / 2;
			int i = mid - 1; lli tmp = p;
			while (i > -1) {
				tmp -= arr[i];
				if (i + 1 >= k) {
					i -= k;
				}
				else {
					i--;
				}
			}
			if (tmp >= 0) {
				ans = mid;
				cost = tmp;
				//indx = mid1;
				lb = mid + 1;
			}
			else ub = mid - 1;
		}

		cout << ans; endl;
	}
	//system("PAUSE");
}


#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
	int t,n,p,k,sum,ans,cnt,pref;
	vector<int> v;

	cin>>t;

	for(int i=0;i<t;i++){
		cin>>n;
		cin>>p;
		cin>>k;
		v.clear();
		v.resize(n);
		for(int j=0;j<n;j++){
			cin>>v[j];
		}
		sum=0;
		cnt=0;
		ans=0;
		pref=0;

		sort(v.begin(),v.end());

		for(int j=0;j<k;j++){

			cnt=j;
			sum=pref;
			if(sum>p){
				break;
			}
			for(int l=j+k-1;l<n;l=l+k){
				if(sum+v[l]<=p){
					cnt=cnt+k;
					sum=sum+v[l];
				}
				else{
					break;
				}
			}
			ans=max(ans,cnt);
			pref=pref+v[j];
		}

		cout<<ans<<endl;

	}

}


//Белый снег
//Белый porshe
//Белый trash
//В бонге воск
#include <iostream>
#include <locale>
#include <string>
#include <iomanip>
#include <string.h>
#include <unordered_map>
#include <bitset>
#include <vector>
#include <stdlib.h>
#include <clocale>
#include <algorithm>
#include <iterator>
#include <list>
#include <set>
#include <cstddef>
#include <cstdio>
#include <stdio.h>
#include <map>
#include <cmath>
#include <functional>
#include <stack>
#include <type_traits>
#include <queue>
#include <memory>
#include <memory.h>
using namespace std;
#define Kirja_ES ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define ll long long
#define ull unsigned long long
#define str string
#define vi vector<int>
#define vll vector<ll>
#define si set<int>
#define sll set<ll>
#define msi multiset<int>
#define msll multiset<ll>
#define pii pair< int, int>
#define left false
#define right true
ll ans[200013] = {};
ll k;
void solve(){
	ll n, p;
	cin >> n >> p >> k;
	vll in_;
	for (int i = 0; i < n; ++i) {
		ll h; cin >> h;
		in_.pb(h);
	}
	sort(in_.begin(), in_.end());
	ll summ = 0;
	for (int i = 1; i < k; ++i) {
		summ += in_.at(i - 1);
		ans[i] = summ;
	}
	for (int i = k; i <= n; ++i) {
		ll x = ans[i - 1];
		ll y = ans[i - k];
		ans[i] = min(x + in_.at(i - 1), y + in_.at(i - 1));
	}
	ll pred = 0;
	for (int i = 1; i <= n; ++i) {
		if (ans[i] <= p)
			pred = i;
	}
	cout << pred << "\n";
}

signed main() {
	Kirja_ES;
	int t;
	cin >> t;
	while (t--) {
		solve();
	}
}


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

int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, p, k;
		cin >> n >> p >> k;
		vector <int> a(n);
		for (int i = 0; i < n; i++)
			cin >> a[i];
		sort(a.begin(), a.end());
		int Max = 0;
		for (int i = 0, pre = 0; i < k && pre <= p; i++) {
			int cnt = i;
			for (int j = i + k - 1, sum = pre; j < n && sum + a[j] <= p; j += k) {
				cnt += k;
				sum += a[j];
			}
			pre += a[i];
			Max = max(Max, cnt);
		}
		cout << Max << endl;
	}
	return 0;
}


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

int n, p, k, a[200000], Max;

bool cmp(int a, int b) {
	return (a > b ? true : false);
}

void shop(int inx, int P, int m) {
	for (int i = inx; i < n; i++) {
		if (a[i] <= P) {
			if (i + k < n) {
				Max = max(Max, m + k);
				int le = i + k, ri = n;
				while (le <= ri) {
					int mid = (le + ri) / 2;
					((a[mid] <= (P - a[i])) ? ri = mid - 1 : le = mid + 1);
				}
				if (Max < ((m + k) + n - le))
					shop(le, P - a[i], m + k);
			}
			else
				Max = max(Max, m + (n - i));
		}
	}
}

int main() {
	int t;
	cin >> t;
	while (t--) {
		cin >> n >> p >> k;
		for (int i = 0; i < n; i++)
			cin >> a[i];
		sort(a, a + n, cmp); //decrease
		Max = 0;
		shop(0, p, 0);
		cout << Max << endl;
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;

int arr[200000];

int main(void)
{
	int t, n, p ,k, temp, ans, g, p_temp, j;
	vector<int> v;
	scanf("%d", &t);
	for (; t > 0; t--)
	{
		scanf("%d %d %d", &n, &p, &k);
		for (int i = 0; i < n; i++)
			scanf("%d", &arr[i]);
		sort(arr, arr + n);

		temp = 0;
		g = 0;
		for (int i = 0; i < k; i++)
		{
			p_temp = p;
			ans = 0;
			if (p_temp >= temp)
			{
				ans += i;
				p_temp -= temp;
			}
			else
				break;

			for (j = i + k - 1; j < n; j += k)
				if (p_temp >= arr[j])
				{
					ans += k;
					p_temp -= arr[j];
				}
				else
					break;
			for (j -= k - 1; j < n; j++)
				if (p_temp >= arr[j])
				{
					ans++;
					p_temp -= arr[j];
				}
				else
					break;
			g = max(ans, g);
			temp += arr[i];
		}
		printf("%d\n", g);
	}
	return 0;
}


#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std;

int arr[200000];

int main(void)
{
	int t, n, p ,k, temp, ans, g, p_temp, j;
	vector<int> v;
	scanf("%d", &t);
	for (; t > 0; t--)
	{
		scanf("%d %d %d", &n, &p, &k);
		for (int i = 0; i < n; i++)
			scanf("%d", &arr[i]);
		sort(arr, arr + n);

		temp = 0;
		g = 0;
		for (int i = 0; i < k; i++)
		{
			p_temp = p;
			ans = 0;
			if (p_temp >= temp)
			{
				ans += i;
				p_temp -= temp;
			}
			else
				break;

			for (j = i + k - 1; j < n; j += k)
				if (p_temp >= arr[j])
				{
					ans += k;
					p_temp -= arr[j];
				}
				else
					break;

			g = max(ans, g);
			temp += arr[i];
		}
		printf("%d\n", g);
	}
	return 0;
}


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

#define ll long long
#define pb push_back
int a[200005],d[200005];
int main(){
	int t;
	cin >> t;
	while(t--)
	{
		int n,p,k;
		cin >> n >>p >> k;
		for(int i=1;i<=n;i++)
		cin >> a[i];
		sort(a+1,a+n+1);
		for(int i=1;i<=n;i++)
		{
			if(i<k) d[i]=d[i-1]+a[i];
			else d[i]=d[i-k]+a[i];
		}
		int ans=0;
		for(int i=1;i<=n;i++)
			if(d[i]<=p) ans=max(ans,i);
		cout<<ans<<"\n";
	}
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <set>

using namespace std;

typedef long long ll;

ll a[200001] = {0};

int main() {
	ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
#else
//	freopen("input.txt","r",stdin);
//	freopen("output.txt", "w",stdout);
#endif
        int t;
	cin >> t;
	for (int q = 0; q < t; q++) {
	        int n, p, k;
	        cin >> n >> p >> k;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		sort(a + 1, a + n + 1);

		int res = 0;
		for (int i = 1; i < k; i++) {
			a[i] += a[i - 1];
			if (a[i] <= p)
				res = i;
		}
		for (int i = k; i <= n; i++)
		{
			a[i] += a[i - k];
			if (a[i] <= p)
				res = i;
		}
		cout << res << endl;
	}
	return 0;
}


#include<stdio.h>
#include<algorithm>
#include<vector>
using namespace std;
int n, m, q;
int a[200009];
int b[200009];
int main() {
	int i, j, k;
	int t;
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d %d", &n, &q, &m);
		for (i = 0; i < n; i++) {
			scanf("%d", &a[i]);
		}
		int res = 0;
		sort(a, a + n);
		b[0] = 0;
		for (i = 1; i < m; i++) {
			b[i] = b[i - 1] + a[i - 1];
			if (b[i] <= q)res = i;
		}
		for (i = m; i <= n; i++) {
			b[i % m] += a[i - 1];
			if (b[i % m] <= q)res = i;
		}
		printf("%d\n", res);
	}
}


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

#define boost ios_base::sync_with_stdio(false);
#define endl '\n'
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<ll, ll>
#define f(i, a, b) for (ll i = (ll)(a); i <= (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i >= (ll)(b); i--)
#define ms(a, b) memset((a), (b), sizeof(a))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))

#define abs(x) ((x < 0) ? (-(x)) : (x))
#define MAX 100005
#define inf LLONG_MAX
#define MIN INT_MIN
#define limit 100000005

//typedef
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;

int mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll powmod(ll a, ll b)
{
    ll res = 1;
    if (a >= mod)
        a %= mod;
    for (; b; b >>= 1)
    {
        if (b & 1)
            res = res * a;
        if (res >= mod)
            res %= mod;
        a = a * a;
        if (a >= mod)
            a %= mod;
    }
    return res;
}

/*..................................................................................................................................*/

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

    int t;
    cin >> t;

    while (t--)
    {
        int n, k, p;
        cin >> n >> p >> k;

        int *price = new int[n];
        for (int i = 0; i < n; i++)
        {
            cin >> price[i];
        }
        sort(price, price + n);

        ll pref = 0;
        ll ans = 0;
        for (int i = 0; i <= k; i++)
        {
            ll sum = pref;
            ll cnt = i;
            if (sum > p)
                break;
            for (int j = i + k - 1; j < n; j += k)
            {
                if (sum + price[j] <= p)
                {
                    cnt += k;
                    sum += price[j];
                }
                else
                {
                    break;
                }
            }

            ans = max(ans, cnt);
            pref += price[i];
        }

        cout << ans << endl;
    }
    return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
int main() {
	int T; cin >> T; for (int d = 0; d < T; d++) {
		int n, q, k,h=0; cin >> n >> q >> k;
		map<int, int> aa; aa[0] = 0;
		vector<int> a(n);
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		sort(a.begin(), a.end());
		int nn = 0;
		for (int i = 0; i < k-1; i++) {
			nn += a[i];
			if (q >= nn) { aa[i + 1] = nn; }

		}
		if (k<=n&&q >= a[k - 1])aa[k] = a[k - 1];
		int ans=0;
		for (int i = 1;i<=n;i++ ) {
			if (aa.count(i)) {
				if (i+k-1<n&&a[i + k - 1] <= q - aa[i])aa[i + k] = aa[i] + a[i + k - 1];
			}
		}

		for (int i = 1; i <= n; i++) {
			if (aa.count(i))ans = i;
		}
		cout << ans << endl;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <algorithm>
#include <vector>
#define Loop int loop = 0; loop < t; ++loop
#define LoopN int i = 0; i < n; ++i
#define LoopM int j = 0; j < m; ++j
#define ll long long
#define Mod1 1000000007
#define Mod2 998244353
using namespace std;

int main()
{
	int t;
	cin >> t;
	for (Loop)
	{
		int n, p, k;
		cin >> n >> p >> k;
		int a[300000];
		int sum[300000] = { 0 };
		int maax[300000] = { 0 };
		for (LoopN)
			cin >> a[i];
		sort(a, a + n);
		/*int Ks[300000] = { 0 };
		for (int i = k - 1; i < n; i += k)
		{
			if (p >= a[i])
			{
				p -= a[i];
				for (int j = 0; j < k; ++j)
					Ks[j] += a[i + j];
			}
		}*/
		int ans = 0;
		for (int i = 1; i < k; ++i)
		{
			if (sum[i - 1] + a[i-1] <= p)
			{
				sum[i] = sum[i - 1] + a[i-1];
				maax[i] = maax[i - 1] + 1;
				ans = i;
			}
		}
		int L = k+1;
		int h = k;
		for (int i = k - 1; i < n; i += k)
		{
			int l = k;
			int r = k;
			int first = k;
			for (int j = 0; j < h; ++j)
			{
				if (i + j < n && sum[j] + a[i + j] <= p)
				{
					maax[j] += k;
					sum[j] += a[i + j];
					L = j + 1;
				}
				else
				{
					if (l == k)
						l = j;
					r = j;
				}
			}
			h = L;
			if(L!=k+1)
				ans = L-1;
			if (l == 0)
			{
				break;
			}
		}
		cout << maax[ans] << '\n';
	}
}


#include <iostream>
#include <string>
#include <math.h>
#include <map>
#include <algorithm>
#include <vector>
#define Loop int loop = 0; loop < t; ++loop
#define LoopN int i = 0; i < n; ++i
#define LoopM int j = 0; j < m; ++j
#define ll long long
#define Mod1 1000000007
#define Mod2 998244353
using namespace std;

int main()
{
	int t;
	cin >> t;
	for (Loop)
	{
		int n, p, k;
		cin >> n >> p >> k;
		int a[300000];
		int sum[300000] = { 0 };
		int maax[300000] = { 0 };
		for (LoopN)
			cin >> a[i];
		sort(a, a + n);
		/*int Ks[300000] = { 0 };
		for (int i = k - 1; i < n; i += k)
		{
			if (p >= a[i])
			{
				p -= a[i];
				for (int j = 0; j < k; ++j)
					Ks[j] += a[i + j];
			}
		}*/
		for (int i = 1; i < k; ++i)
		{
			if (sum[i - 1] + a[i-1] <= p)
			{
				sum[i] = sum[i - 1] + a[i-1];
				maax[i] = maax[i - 1] + 1;
			}
		}
		int L = k;
		for (int i = k - 1; i < n; i += k)
		{
			int l = k;
			int h = L;
			for (int j = 0; j < h; ++j)
			{
				if (i + j < n && sum[j] + a[i + j] <= p)
				{
					maax[j] += k;
					sum[j] += a[i + j];
					L = j + 1;
				}
				else
				{
					if (l == k)
						l = j;
				}
			}
			if (l == 0)
			{
				break;
			}
		}
		int ans = 0;
		for (int i = 1; i < k; ++i)
			if (maax[i] > maax[ans])
				ans = i;
		cout << maax[ans] << '\n';
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;
        vector<int> sumv(n, 0);
        for(int b=0; b<n; b++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);

        }
        sort(v.begin(), v.end());
        for(int b=0; b<k; b++)
        {
            if(b == 0)
            {
                sumv[b] = v[b];
            }
            else{
                sumv[b] = sumv[b-1] +v[b];
            }
        }
        // sumv = v;

        int max_result = 0;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                tmp -= sumv[j-1];
                if(tmp>=0)
                {
                    result = j;
                }

            }

            for(int q=j; q+k-1 <n && tmp - v[q+k-1]>=0; q+=k)
            {
                result +=k;
                tmp -= v[q+k-1];
            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int i=0; i<n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+1];
                }
                else{
                    if(v[q]<=tmp)
                    {
                        result+=1;
                        tmp -= v[q];
                    }
                    // break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int i=0; i<n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    while(q < n &&v[q]<=tmp)
                    {
                        result+=1;
                        tmp -= v[q];
                        q++;
                    }
                    break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;
        vector<int> sumv;
        for(int b=0; b<n; b++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
            if(sumv.back())
            {
                sumv.push_back(sumv.back() + tmp);
            }
            else{
                sumv.push_back(tmp);
            }
        }
        sort(v.begin(), v.end());
        // sumv = v;

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0 && sumv[j-1]<=tmp)
            {
                tmp -= sumv[j-1];
                result = j;
            }
            else{
                result = -100;
                // break;
            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(q+k-1 < n &&v[q+k-1] <= tmp)
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int i=0; i<n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    if(v[q]<=tmp)
                    {
                        result+=1;
                        tmp -= v[q];
                    }
                    else{
                        break;
                    }
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int i=0; i<n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                if(v[j - 1] <= tmp)
                {
                    tmp -= v[j-1];
                    result = j;
                }
            }
            for(int q=j; q<n; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+1];
                }
                else{
                    if(v[q]<=tmp)
                    {
                        result+=1;
                    }
                    // break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;
        vector<int> sumv;
        for(int b=0; b<n; b++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
            if(sumv.back())
            {
                sumv.push_back(sumv.back() + tmp);
            }
            else{
                sumv.push_back(tmp);
            }
        }
        sort(v.begin(), v.end());
        // sumv = v;

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0 && sumv[j-1]<=tmp)
            {
                tmp -= sumv[j-1];
                result = j;
            }
            else{
                result = -100;
                break;
            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(q+k-1 < n &&v[q+k-1] <= tmp)
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int b=0; b<n; b++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    while(q < n &&v[q]<=tmp)
                    {
                        result+=1;
                        tmp -= v[q];
                        q++;
                    }
                    break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int b=0; b<n; b++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(q+k-1 < n &&v[q+k-1] <= tmp)
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    while(q < n &&v[q]<=tmp)
                    {
                        result+=1;
                        tmp -= v[q];
                        q++;
                    }
                    break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include "math.h"
using namespace std;

int main(int, char**) {
    int t;
    cin >> t;
    for(int i=0; i<t; i++)
    {
        int n, p, k;
        scanf("%d %d %d",&n ,&p, &k);
        vector<int> v;

        for(int i=0; i<n; i++)
        {
            int tmp;
            scanf("%d", &tmp);
            v.push_back(tmp);
        }
        sort(v.begin(), v.end());

        int max_result = -1;
        for(int j=0; j < k; j++)
        {
            int tmp = p;
            int result = 0;
            if(j-1>=0)
            {
                for(int a=0; a<=j-1 && v[a] <=tmp; a++)
                {
                    if(tmp <= 0)
                    {
                        break;
                    }
                    tmp -= v[a];
                    result += 1;
                }

            }
            for(int q=j; q<n && tmp >0; q+=k)
            {

                if(v[q+k-1] <= tmp && q+k-1 < n )
                {
                    result +=k;
                    tmp -= v[q+k-1];
                }
                else{
                    while(v[q]<=tmp && q < n)
                    {
                        result+=1;
                        tmp -= v[q];
                        q++;
                    }
                    break;
                }

            }
            // printf("result %d", result);
            if(result > max_result)
            {
                max_result = result;
            }
        }

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

    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <algorithm>
#include <time.h>
//#include <math.h>
#include <cmath>
#include <fstream>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <stdlib.h>
#include <iomanip>
#include <cstdio>
#include <cassert>
#include <bitset>

#pragma comment(linker, "/STACK:36777216")
using namespace std;

#define flu fflush(stdout)
#define gg return 0
#define mp make_pair

long long mod = /*27449*/998244353/* 1e9 + 7 /*1e9 + 9*/;
long long inf = 1e9;
long double eps = 1e-7;
long double pi = acosl(-1);
ifstream in("input.txt");
ofstream out("output.txt");

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, k;
		long long p;
		cin >> n >> p >> k;
		vector<int> a;
		for (int i = 0; i < n; i++)
		{
			int t;
			cin >> t;
			a.push_back(t);
		}
		sort(a.begin(), a.end());
		vector<long long> req (n + 1);
		req[0] = 0;
		int mxv = 0;
		for (int i = 1; i <= n; i++)
		{
			if (i < k)
			{
				req[i] = req[i - 1] + a[i - 1];
			}
			else
			{
				req[i] = req[i - k] + a[i - 1];
			}
			if (req[i] <= p)
			{
				mxv = i;
			}
		}
		cout << mxv << "\n";
	}
	gg;
}


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

int main() {
	int t;
	cin >> t;
	int n, p, k;
	vector<int> gifts;
	for (int i = 0; i < t; ++i) {
		cin >> n >> p >> k;
		gifts.resize(n);
		for (int j = 0; j < n; ++j)
			cin >> gifts[j];
		sort(gifts.begin(), gifts.end());
		int money = p, q = 0;
		for (int j = k - 1; j < n; j += k) {
			if (money >= gifts[j]) {
				++q;
				money -= gifts[j];
			}
			else
				break;
		}
		int sum;
		int quant = q * k;
		for (int j = 0; j < k - 1; ++j) {
			if (k * q + j >= n)
				break;
			sum = 0;
			for (int l = 0; l <= j; ++l) {
				sum += gifts[l];
			}
			for (int l = 1; l <= q; ++l) {
				sum += gifts[k * l + j];
				if (sum > p)
					break;
			}
			if (sum <= p) {
				quant = q * k + j + 1;
			}
		}
		cout << quant << '\n';
	}
	//system("pause");
	return 0;
}


//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
typedef long long ll;
const int maxn=1e5+9;
const int mod=1e9+7;
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,p,k,i;
		scanf("%d%d%d",&n,&p,&k);
		int a[maxn]={0};
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		sort(a+1,a+1+n);
		int sum[maxn]={0},sumk[maxn]={0};
		for(i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(i>=k)sumk[i]=sumk[i-k]+a[i];
			else sumk[i]=a[i];
		}
		int ans=0;
		for(i=1;i<=n;i++)
		{
			int s;
			if(i>=k)
			{
				if(i%k==0)s=sumk[i];
				else s=sumk[i]+sum[i%k-1];
			}
			else s=sum[i];
			if(s<=p)ans=i;
		}
		printf("%d\n",ans);
	}
}


//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
#include<iostream>
typedef long long ll;
const int maxn=2e5+9;
const int mod=1e9+7;
using namespace std;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,p,k,i;
		scanf("%d%d%d",&n,&p,&k);
		int a[maxn]={0};
		for(i=1;i<=n;i++)scanf("%d",&a[i]);
		sort(a+1,a+1+n);
		int sum[maxn]={0},sumk[maxn]={0};
		for(i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(i>=k)sumk[i]=sumk[i-k]+a[i];
			else sumk[i]=a[i];
		}
		int ans=0;
		for(i=1;i<=n;i++)
		{
			int s;
			if(i>=k)
			{
				if(i%k==0)s=sumk[i];
				else s=sumk[i]+sum[i%k-1];
			}
			else s=sum[i];
			if(s<=p)ans=i;
		}
		printf("%d\n",ans);
	}
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int solve(int n, int p, int k, const vector<int> &a)
{
	vector<int> b(k);
	int r = 0;
	for (int i = 1; i < k; ++i)
	{
		b[i] = b[i-1] + a[i-1];
		if (b[i] <= p)
			r = i;
	}
	for (int i = k; i < n + 1; ++i)
	{
		b[i%k] += a[i-1];
		if (b[i%k] <= p)
			r = max(r, i);
	}
	return r;
}

const int MAXA = 1e4;
const int MAXT = 1e4;
const int MAXN = 2e5;

int was[MAXA+2];
int b[MAXT][3];
vector<int> a[MAXT];
pair<int,int> all[MAXN];

int main()
{
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; ++i)
	{
		scanf("%d%d%d", &b[i][0], &b[i][1], &b[i][2]);
		a[i].reserve(b[i][0]);
		for (int j = 0; j < b[i][0]; ++j)
		{
			int x;
			scanf("%d", &x);
			a[i].push_back(x);
			was[x] += 1;
		}
		//sort(a[i].begin(),a[i].end());
		//printf("%d\n", solve(b[i][0], b[i][1], b[i][2], a[i]));
	}
	//return 0;
	for (int i = 1; i < MAXA+2; ++i)
		was[i] += was[i-1];
	for (int i = 0; i < t; ++i)
	{
		for (int j = 0; j < a[i].size(); ++j)
		{
			int x = a[i][j];
			all[was[x-1]] = pair<int,int>(x, i);
			was[x-1] += 1;
		}
		a[i].clear();
	}
	for (int i = 0; i < was[MAXA+1]; ++i)
	{
		const pair<int,int> &p = all[i];
		a[p.second].push_back(p.first);
	}
	for (int i = 0; i < t; ++i)
		printf("%d\n", solve(b[i][0], b[i][1], b[i][2], a[i]));
	return 0;
}


#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int solve(int n, int p, int k, const vector<int> &a)
{
	vector<int> b(k);
	int r = 0;
	for (int i = 1; i < k; ++i)
	{
		b[i] = b[i-1] + a[i-1];
		if (b[i] <= p)
			r = i;
	}
	for (int i = k; i < n + 1; ++i)
	{
		b[i%k] += a[i-1];
		if (b[i%k] <= p)
			r = max(r, i);
	}
	return r;
}

const int MAXA = 1e4;
const int MAXT = 1e4;
const int MAXN = 2e5;

int was[MAXA+2];
int b[MAXT][3];
vector<int> a[MAXT];
pair<int,int> all[MAXN];

int main()
{
	int t;
	scanf("%d", &t);
	for (int i = 0; i < t; ++i)
	{
		scanf("%d%d%d", &b[i][0], &b[i][1], &b[i][2]);
		a[i].reserve(b[i][0]);
		for (int j = 0; j < b[i][0]; ++j)
		{
			int x;
			scanf("%d", &x);
			a[i].push_back(x);
			was[x] += 1;
		}
		sort(a[i].begin(),a[i].end());
		printf("%d\n", solve(b[i][0], b[i][1], b[i][2], a[i]));
	}
	return 0;
	for (int i = 1; i < MAXA+2; ++i)
		was[i] += was[i-1];
	for (int i = 0; i < t; ++i)
	{
		for (int j = 0; j < a[i].size(); ++j)
		{
			int x = a[i][j];
			all[was[x-1]] = pair<int,int>(x, i);
			was[x-1] += 1;
		}
		a[i].clear();
	}
	for (int i = 0; i < was[MAXA+1]; ++i)
	{
		const pair<int,int> &p = all[i];
		a[p.second].push_back(p.first);
	}
	for (int i = 0; i < t; ++i)
		printf("%d\n", solve(b[i][0], b[i][1], b[i][2], a[i]));
	return 0;
}


#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996)

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;
using namespace std;

int main(int argc, char* argv[])
{
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        int n; ll p; int k;
        scanf("%d%lld%d", &n, &p, &k);

        vector<int> a(n),s(n+1);
        int i;
        for(i=0; i<n; i++) {
            scanf("%d", &a[i]);
        }
        sort(a.begin(), a.end());
        for(i=0; i<n; i++) {
            s[i+1]=s[i]+a[i];
        }

        ll ans=0;
        int q;
        for(q=0; q<k; q++) {
            ll sum0=s[q];
            if(sum0<=p) {
                ans=MAX(ans,q);
            }
            for(i=q+k-1; i<n; i+=k) {
                sum0+=a[i];
                if(sum0<=p) {
                    ans=MAX(ans,i+1);
                }
            }
        }
        printf("%lld\n", ans);

    }

    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

long long dp[200055][2];
int n,k;
long long p;
long long cost[200055];
long long INF=1e12;

long long ans(int now,int type) {
    if (dp[now][type]!=-INF) {
        return dp[now][type];
    }
    if (type==1&&now<k-1) {
        return -INF;
    }
    if (type==1&&now==k-1) {
        dp[now][type]=p-cost[now];
        return dp[now][type];
    }
    if (now==0) {
        return p-cost[0];
    }
    if (type==0) {
        dp[now][type]=max(ans(now-1,0),ans(now-1,1))-cost[now];
    }
    if (type==1) {
        dp[now][type]=max(ans(now-k,0),ans(now-k,1))-cost[now];
    }
    return dp[now][type];
}

long long money(int now) {
    if (now==-1) {
        return INF;
    }
    if (now==n) {
        return -INF;
    }
    return max(ans(now,0),ans(now,1));
}

int search() {
    int ret;
    for(int i=-1;i<n;i++) {
        if (money(i)>=0) {
            ret=i+1;
        }
    }
    return ret;
}

int main(void) {
    int c;
    scanf("%d\n",&c);
    for(int i=0;i<200055;i++) {
        dp[i][0]=-INF;
        dp[i][1]=-INF;
    }
    for(int t=0;t<c;t++) {
        scanf("%d %lld %d\n",&n,&p,&k);
        for(int i=0;i<n;i++) {
            scanf("%lld ",&cost[i]);
        }
        sort(cost,cost+n);
        printf("%d\n",search());
        for(int i=0;i<=n;i++) {
            dp[i][0]=-INF;
            dp[i][1]=-INF;
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int arr[200010];
int dp[200010];

int main()
{
	int t;
	int n , p , k;
	int i , j;

	scanf("%d",&t );

	for( i = 0 ; i < t ; i++ )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		for( j = 1 ; j <= n ; j++ )
			scanf("%d",&arr[j] );

		arr[0] = 0;

		sort( arr , arr + n + 1 );

		dp[0] = 0;

		int ans = 0;

		for( j = 1 ; j <= n ; j++ )
		{
			if( j - k < 0 )
				dp[j] = dp[j-1] + arr[j];
			else
				dp[j] = dp[j-k] + arr[j];

			if( dp[j] <= p )
				ans = j;
		}

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


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			if( dp[i] < 0 )
				break;
		}

		if( i < k && i != 0)
			printf("1\n");
		else
			printf("%d\n",i-1);

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
			printf("%d ",pt[i]);
			printf("\n");
		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			if( dp[i] < 0 )
				break;
		}

		if( i < k && i != 0)
			printf("1\n");
		else
			printf("%d\n",i-1);

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			if( dp[i] < 0 )
				break;
		}
		printf("%d\n",i-1);

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int gd[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k ;
	int i , j;
	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		for( i = 1 ; i <= n ; i++ )
			scanf("%d", &gd[i] );

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

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - gd[i];
			else
				dp[i] = dp[i-k] - gd[i];

			if( dp[i] < 0 )
				break;
		}

		i--;

		if( i < k )
		{
			int sum = 0;

			for( j = 1 ; j <= i ; j++ )
			{
				sum += gd[j];

				if( sum > p )
					break;
			}

			i = j - 1;
		}

		printf("%d\n",i);
		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int gd[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k ;
	int i , j , h ;
	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		for( i = 1 ; i <= n ; i++ )
			scanf("%d", &gd[i] );

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

		dp[0] = 0;
		int ans = 0;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = gd[i] + dp[i-1];
			else
				dp[i] = dp[i-k] + gd[i];

			if( dp[i] <= p )
				ans = i;
		}

		printf("%d\n",ans);
		t--;
	}
}
/*
5 6 2
2 4 3 5 7
*/


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			if( dp[i] < 0 )
				break;
		}

		if( i <= k && i != 0)
			printf("1\n");
		else
			printf("%d\n",i-1);

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;
	int sum;
	int a1 , a2;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;
		sum = p;
		a1 = 0;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			sum -= pt[i];

			if( sum >= 0 )
				a1 = i;

			if( dp[i] < 0 )
				break;
		}

		if( i <= k && i != 1 )
			a2 = 1;
		else
			a2 = i - 1;

		printf("%d\n",max( a1 , a2) );

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int pt[200100];
int dp[200100];

int main()
{
	int t;
	int n , p , k;
	int i;

	scanf("%d",&t);

	while( t > 0 )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		pt[0] = 0;

		for( i = 1 ; i <= n ; i++ )
			scanf("%d",&pt[i] );

		sort( pt , pt + n + 1 );

		dp[0] = p;

		for( i = 1 ; i <= n ; i++ )
		{
			if( i - k < 0 )
				dp[i] = dp[0] - pt[i];
			else
				dp[i] = dp[ i - k ] - pt[i];

			if( dp[i] < 0 )
				break;
		}

		if( i <= k && i != 1 )
			printf("1\n");
		else
			printf("%d\n",i-1);

		t--;
	}
}


#include<stdio.h>
#include<algorithm>

using namespace std;

int arr[200010];
int dp[200010];

int main()
{
	int t;
	int n , p , k;
	int i , j;

	scanf("%d",&t );

	for( i = 0 ; i < t ; i++ )
	{
		scanf("%d %d %d",&n ,&p ,&k );

		for( j = 1 ; j <= n ; j++ )
			scanf("%d",&arr[j] );

		arr[0] = 0;

		sort( arr , arr + n + 1 );

		dp[0] = 0;

		for( j = 1 ; j <= n ; j++ )
		{
			if( j - k < 0 )
				dp[j] = dp[j-1] + arr[j];
			else
				dp[j] = dp[j-k] + arr[j];

			if( dp[j] > p )
				break;
		}

		j--;

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


///Bismillahir Rahmanir Rahim

#include<cstdio>
#include<iomanip>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<stack>
#include<list>
#include<iostream>
#include<assert.h>
#include<bits/stdc++.h>

/**Define file I/O **/
#define f_input freopen("input.txt","r",stdin)
#define f_output freopen("output.txt","w",stdout)

/**Define memory set function**/
#define mem(x,y) memset(x,y,sizeof(x))
#define CLEAR(x) memset(x,0,sizeof(x))

/**Define function and object**/
#define pb push_back
#define Sort(v) sort(v.begin(),v.end())
#define RSort(v) sort(v.rbegin(),v.rend())
#define CSort(v,C) sort(v.begin(),v.end(),C)
#define all(v) (v).begin(),(v).end()
#define sqr(x) ((x)*(x))
#define find_dist(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))

/**Define constant value**/
#define ERR 1e-9
#define pi (2*acos(0))
#define PI 3.141592653589793

/**Define input**/
#define scanint(a) scanf("%d",&a)
#define scanLLD(a) scanf("%lld",&a)
#define scanstr(s) scanf("%s",s)
#define scanline(l) scanf(" %[^\n]",l)
#define scandouble(d) scanf("%lf",&d)
#define scanchar(c) scanf("%c",&c)

/**Define Bitwise operation**/
#define check(n, pos) (n & (1ll<<(pos)))
#define biton(n, pos) (n | (1ll<<(pos)))
#define bitoff(n, pos) (n & ~(1ll<<(pos)))

/**Define color**/
enum {WHITE,GREY,BLACK};

/**Sync off with stdio**/
#define __ cin.sync_with_stdio(false);\
           cin.tie();

/**Debug tools**/
#define what_is(x) cerr<<(#x)<<" is "<<x<<endl
using namespace std;

/**Typedef**/
typedef vector<int> vint;
typedef vector< vint > vint2D;
typedef vector<string> vstr;
typedef vector<char>vchar;
typedef vector< vchar >vchar2D;
typedef queue<int> Qi;
typedef queue< Qi > Qii;
typedef map<int,int> Mii;
typedef map<string,int> Msi;
typedef map<int,string> Mis;
typedef stack<int> stk;
typedef pair<int,int> pp;
typedef pair<int, pp > ppp;
typedef long long int ll;
ll inf=1e18;

/**Template & structure**/

template<class T>T gcd(T a,T b){return b == 0 ? a : gcd(b, a % b);}

template<typename T>T lcm(T a, T b) {return a / gcd(a,b) * b;}

template<typename T>T last_bit(T n) { return n & 1; }

template<class T>T big_mod(T n,T p,T m){if(p==0)return (T)1;T x=big_mod(n,p/2,m);x=(x*x)%m;if(p&1)x=(x*n)%m;return x;}

template<class T>T modInv(T a, T m){T x, y; extgcd(a, m, x, y); x %= m; while (x < 0){x += m;} return x;}

template<class T> T extgcd(T a,T b,T& x,T& y){if(b==0){x=1;y=0;return a;}else{int g=extgcd(b,a%b,y,x);y-=a/b*x;return g;}}

template<class T>T multiplication(T n,T p,T m){if(p==0)return (T)0;T x=multiplication(n,p/2,m);x=(x+x)%m;if(p&1)x=(x+n)%m;return x;}

template<class T>T my_pow(T n,T p){if(p==0)return 1;T x=my_pow(n,p/2);x=(x*x);if(p&1)x=(x*n);return x;} ///n to the power p

template <class T> double getdist(T a, T b){return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));}/// distance between a & b

template <class T> T extract(string s, T ret) {stringstream ss(s); ss >> ret; return ret;}/// extract words or numbers from a line

template <class T> string tostring(T n) {stringstream ss; ss << n; return ss.str();}/// convert a number to string

template<class T> T Mod(T n,T m) {return (n%m+m)%m;} ///For Positive Negative No.

template<class T> T MIN3(T a,T b,T c) {return min(a,min(b,c));} /// minimum of 3 number

template<class T> T MAX3(T a,T b,T c) {return max(a,max(b,c));} ///maximum of 3 number

template <class T> void print_vector(T &v){int sz=v.size();if(sz)cout<<v[0];for(int i = 1; i < sz; i++)cout << ' '<<v[i];cout<<"\n";}/// prints all elements in a vector

bool isVowel(char ch){ ch=toupper(ch); if(ch=='A'||ch=='U'||ch=='I'||ch=='O'||ch=='E') return true; return false;}

bool isConsonant(char ch){if (isalpha(ch) && !isVowel(ch)) return true; return false;}

template <class R> R Josephus(R n,R k){R ans=1;for(R i=2;i<=n;i++)ans=(ans+k-1)%i+1;return ans;}

template <class R> R toitent_Phi2(R a){R result = a;for(R i=2;i*i<=a;i++){if(a%i==0) result=result-result/i;while(a%i==0) a=a/i;}if(a>1) result=result-result/a;return result;}

template <typename T> T Angle(T x1,T y1,T x2, T y2){ return atan( double(y1-y2) / double(x1-x2));}

//namespace debug{
//    int sum(){return 0;}
//    template<typename T,typename... Args> T sum(T a,Args... args) {return a+sum(args...);}
//    void print(){cout<<"\n";return;}template<typename T, typename... Args>void print(T a,Args... args){cout<<a<<" ";print(args...);}
//}

/**Direction**/
///int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1}; ///8 Direction
///int dx[4] = {1, 0, -1, 0};int dy[4] = {0, 1, 0, -1}; ///4 Direction
///int dx[]={2,1,-1,-2,-2,-1,1,2};int dy[]={1,2,2,1,-1,-2,-2,-1};///Knight Direction
///int dx[]={-1,-1,+0,+1,+1,+0};int dy[]={-1,+1,+2,+1,-1,-2}; ///Hexagonal Direction

/******************************End******************************/

#define mxn 300009
vector<ll>G[mxn],E[mxn];
ll a[mxn];
ll city[mxn];
ll vis[mxn];
ll lvl[mxn];
ll par[mxn];
set<ll>ans;

void bfs(ll src,ll mx){
    vis[src]=1;
    queue<ll>Q;
    Q.push(src);
    lvl[src]=0;

    while(!Q.empty()){
        ll u=Q.front();
        Q.pop();

        if(lvl[u]==mx) return;

        ll sz=G[u].size();
        for(ll i=0;i<sz;i++){
            ll v=G[u][i];

            if(par[u]==v) continue;

            if(a[v]==1){
                ans.insert(E[u][i]);
                //vis[v]=1;
                continue;
            }

            if(vis[v]==1){
                ans.insert(E[u][i]);
                continue;
            }

            if(vis[v]==0){
                vis[v]=1;
                par[v]=u;
                lvl[v]=lvl[u]+1;
                Q.push(v);
            }

        }
    }
}

int main()
{
    __;

    ll n,m,q;
    ll i,j,k;

    cin>>n>>m>>q;
    for(i=1;i<=m;i++){
        ll d;
        cin>>d;
        a[d]=1;
        city[i]=d;
        par[d]=d;
    }

    k=1;
    for(i=1;i<n;i++){
        ll u,v;
        cin>>u>>v;
        G[u].push_back(v);
        G[v].push_back(u);
        E[u].push_back(k);
        E[v].push_back(k);
        k++;
    }

    for(i=1;i<=m;i++){
        ll c=city[i];
        if(vis[c]==0){
            bfs(c,q);
        }
    }

    cout<<ans.size()<<"\n";
    for(auto it=ans.begin();it!=ans.end();it++) cout<<*it<<" ";
    cout<<"\n";

    return 0;
}