package javaPractices;

import java.util.Scanner;

public class prac1 {

	public static Scanner mySc = new Scanner(System.in);

	public static void main(String[] args) {
		int row = 0, col = 0;
		int flag = 0;

		String msString= mySc.nextLine();
		String[] splitStr = msString.split("\\s+");

		row= Integer.valueOf(splitStr[0]);
		col= Integer.valueOf(splitStr[1]);

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

			if (i % 2 == 1) {
				for (int x = 0; x < col; x++)
					System.out.print("#");
				System.out.println("");
			} else {
				if (flag % 2 == 0) {
					for (int x = 0; x < col - 1; x++)
						System.out.print(".");
					System.out.println("#");
					flag++;
				} else {
					System.out.print("#");
					for (int x = 0; x < col - 1; x++)
						System.out.print(".");
					System.out.println("");
					flag++;
				}

			}

		}

	}
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);

        for(int i=0;i<n/2;i++) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }

            System.out.println();

            for(int k=0;k<m-1;k++) {
                   System.out.print(".");
            }

            System.out.print("#");
            System.out.println();

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }

        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c!=n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c-1%4==0) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c<n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if((c-1)%4==0 && c<n) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else if((c-1)%4==2 && c<n) {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c<=n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c-1%4==0) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c<n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c>n) {
                break;
            }

            if(c-1%4==0) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c<n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c-1%4==0 && c<n) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else if(c-1%4==2 && c<n) {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);

        for(int i=0;i<n/2;i++) {
            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
            System.out.println();
            for(int k=0;k<m-1;k++) {
                   System.out.print(".");
            }
            System.out.print("#");
            System.out.println();
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);

        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                System.out.print("#");
                System.out.println();
            }

            for(int k=0;k<m-1;k++) {
                   System.out.print(".");
            }
            System.out.print("#");
            System.out.println();
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c!=n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c-1%4==0) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.io.*;
public final class hello {
    public static void main(String args[])throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

        String a[]=new String[2];
        a=br.readLine().split(" ");

        int n=Integer.parseInt(a[0]);
        int m=Integer.parseInt(a[1]);
        int c=0;

        while(c<n) {

            for(int j=0;j<m;j++) {
                System.out.print("#");
             }
             c++;

            System.out.println();

            if(c-1%4==0) {
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.print("#");
                System.out.println();
                c++;
            }
            else {
                System.out.print("#");
                for(int k=0;k<m-1;k++) {
                       System.out.print(".");
                }

                System.out.println();
                c++;
            }
        }

    }
}


import java.util.Scanner;
public class Snake
{
	public static void main (String [] args)
	{
		Scanner sc=new Scanner (System.in);
		int r=sc.nextInt();
		int c=sc.nextInt();
		for (int i=0;i<r;i++)
		{
			for (int j=0;j<c;j++)
				if (i%2==0 || j==c-1)
					System.out.print('#');
				else
					System.out.print('.');
			System.out.println();
		}

	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class FoxandSnake {

	public static void main(String[] args) throws IOException {
		BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
		StringTokenizer st=new StringTokenizer (br.readLine());
		int r=Integer.parseInt(st.nextToken());
		int c=Integer.parseInt(st.nextToken());

		StringBuilder sb=new StringBuilder ();
		for (int i=0;i<r;i++)
		{
			for (int j=0;j<c;j++)
				if (i%2==0)
					sb.append("#");
				else if (i%4==1 && j==c-1)
					sb.append("#");
				else if (i%4==3 && j==0)
					sb.append("#");
				else
					sb.append(".");

			sb.append("\n");
		}
		System.out.print(sb);

	}

}


import java.util.Scanner;
public class Snake
{
	public static void main (String [] args)
	{
		Scanner sc=new Scanner (System.in);
		int r=sc.nextInt();
		int c=sc.nextInt();
		for (int i=0;i<r;i++)
		{
			for (int j=0;j<c;j++)
				if ((i%2==0 )|| ((j==c-1)&&(i%4==1)) ||((j==0) &&(i%4==3)))
					System.out.print('#');
				else
					System.out.print('.');
			System.out.println();
		}

	}
}


import java.util.Scanner;

public class _510AFoxAndSnake {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int count = in.nextInt(), length = in.nextInt();
        StringBuilder snake = new StringBuilder(length);
        StringBuilder tail = new StringBuilder(length);
        length--;
        for (int i = 0; i < length; i++) {
            snake.append('#');
            tail.append('.');
        }
        snake.append('#');
        tail.append('#');

        StringBuilder tailReversed = new StringBuilder(tail);
        tailReversed.reverse();

        while (count != 0) {
            System.out.println(snake);
            count--;
            if (count == 0) break;
            System.out.println(tail);
            count--;
            if (count == 0) break;
            System.out.println(snake);
            count--;
            if (count == 0) break;
            System.out.println(tailReversed);
            count--;
        }
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class foxAnsSnake510a {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int line = in.nextInt();
        int lineLength = in.nextInt();
        boolean t = true;
        boolean y;
        for (int i = 0; i < line; i++) {
            y = i % 2 == 0;
            for (int j = 0; j < lineLength; j++) {
                if (y) {
                    out.print('#');
                } else {
                    if (t) {
                       if (j == lineLength-1) out.print('#');
                       else out.print('.');
                       t = false;
                    } else {
                        if (j == 0) out.print('#');
                        else out.print(".");
                        t = true;
                    }
                }
            }
            out.println();
        }
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class foxAnsSnake510a {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int line = in.nextInt();
        int lineLength = in.nextInt();
        char[][] snake = new char[line][lineLength];

        for (int i = 0; i < snake.length; i++) {
            for (int j = 0; j < lineLength; j++) {
                snake[i][j] = '#';
            }
            if (++i == line) break;
            for (int j = 0; j < lineLength; j++) {
                snake[i][j] = '.';
            }
        }
        for (int i = 1; i < line; i += 2) {
            snake[i][lineLength - 1] = '#';
            i += 2;
            if (i >= line) break;
            snake[i][0] = '#';
        }
        for (int i = 0; i < line; i++) {
            out.println(snake[i]);
        }
        out.close();
    }
}


import java.io.PrintWriter;
import java.util.Scanner;

public class foxAnsSnake510a {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        int line = in.nextInt();
        int lineLength = in.nextInt();
        boolean t = true;
        boolean y = true;
        for (int i = 0; i < line; i++) {
            for (int j = 0; j < lineLength; j++) {
                if (y) {
                    out.print('#');
                } else {
                    if (t) {
                        if (j == lineLength - 1) out.print('#');
                        else out.print('.');
                    } else {
                        if (j == 0) out.print('#');
                        else out.print('.');
                    }
                }
            }
            y = !y;
            if (y) t = !t;
            out.println();
        }
        out.close();
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

public class Lo {
    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int m = sc.nextInt();
      for(int i = 0; i < n; i++){
          for(int j = 0; j < m; j++){
              if(i%2==0){
                  System.out.print('#');
              }
              else if((i-1)%4==0){
                  if(j==m-1){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
              else if((i-4)%4==0){
                  if(j==0){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
          }
      }
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

public class Lo {
    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int m = sc.nextInt();
      for(int i = 0; i < n; i++){
          for(int j = 0; j < m; j++){
              if(i%2==0){
                  System.out.print('#');
              }
              else if((i-1)%4==0){
                  if(j==m-1){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
              else if((i-4)%4==0){
                  if(j==0){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
              System.out.println();
          }
      }
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

public class Lo {
    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int m = sc.nextInt();
      for(int i = 0; i < n; i++){
          for(int j = 0; j < m; j++){
              if(i%2==0){
                  System.out.print('#');
              }
              else if((i-1)%4==0){
                  if(j==m-1){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
              else if((i-4)%4==0){
                  if(j==0){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
          }
          System.out.println();
      }
    }
}


import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.ArrayList;
import java.util.Collections;
import java.lang.Math;

public class Lo {
    public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      int n = sc.nextInt();
      int m = sc.nextInt();
      for(int i = 0; i < n; i++){
          for(int j = 0; j < m; j++){
              if(i%2==0){
                  System.out.print('#');
              }
              else if((i-1)%4==0){
                  if(j==m-1){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
              else if((i-3)%4==0){
                  if(j==0){
                      System.out.print('#');
                  }
                  else{
                      System.out.print('.');
                  }
              }
          }
          System.out.println();
      }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.PrintWriter;
import java.math.BigInteger;

public class Main {

    static void mostrar(String A[][], int n, int m) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                System.out.print(A[i][j]);
            }
            System.out.println();
        }
    }

    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner();
        int n = Integer.parseInt(sc.next());
        int m = Integer.parseInt(sc.next());
        String A[][] = new String[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                A[i][j] = ".";
            }
        }
        int v = n + (2 * m - 2);
        int x = 0;
        int y = 0;
        boolean cond = false;
        boolean cond2 = true;
        boolean cond3 = false;
        while (v != 0) {
            v--;
            A[x][y] = "#";

            if (x == n - 1 && y == m - 1) {
                cond = true;
            }
            if (x == n - 1) {
                cond3 = false;
                cond2 = true;
            }
            if (y == m - 1) {
                cond2 = false;
                cond3 = true;
            }
            if (!cond3 && cond2 && !cond) {
                y++;
                cond2 = true;
                cond3 = false;
            }
            if (cond3 && !cond2 && !cond) {
                x++;
                cond3 = true;
                cond2 = false;
            }
            if (cond) {
                y--;
            }

        }
        mostrar(A, n, m);
    }

    static class Scanner {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");
        int spaces = 0;

        public String nextLine() throws IOException {
            if (spaces-- > 0) {
                return "";
            } else if (st.hasMoreTokens()) {
                return new StringBuilder(st.nextToken("\n")).toString();
            }
            return br.readLine();
        }

        public String next() throws IOException {
            spaces = 0;
            while (!st.hasMoreTokens()) {
                st = new StringTokenizer(br.readLine());
            }
            return st.nextToken();
        }

        public boolean hasNext() throws IOException {
            while (!st.hasMoreTokens()) {
                String line = br.readLine();
                if (line == null) {
                    return false;
                }
                if (line.equals("")) {
                    spaces = Math.max(spaces, 0) + 1;
                }
                st = new StringTokenizer(line);
            }
            return true;
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pii;
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define endl "\n"
ll a[200005];
ll sum[200005]={};
int main()
{
	ios;
	ll t,n,p,k,f,ptr,d,diff,i,max1,z;
	cin>>t;
	for(z=1;z<=t;z++)
	{
		max1=0;f=0;
		cin>>n>>p>>k;
		for(i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+n+1);
		for(i=1;i<=n;i++)
			sum[i]=sum[i-1]+a[i];
		ptr=0;
		while(p>0&&(ptr+k)<=n)
		{
			d=a[ptr+k];
			if(d<=p)
			{
				ptr+=k;
				p-=d;
				f=1;
			}
			else
				break;
		}
		if(f!=1)
		{
			while(p>0&&ptr<n)
			{
				d=a[ptr+1];
				if(d<=p)
				{
					ptr++;
					p-=d;
				}
				else
					break;
			}
			cout<<ptr<<endl;
		}
		else
		{
			max1=ptr;
			for(i=min(ptr+k-1,n);i>=ptr+1;i--)
			{
				ll q=i-ptr;
				ll res=p+a[k]-a[i];
				if(res>0)
				{
					d=min((ll)(lower_bound(sum,sum+q+1,res)-sum),(ll)(q));
					//cout<<d<<endl;
					max1=max(ptr+d,max1);
				}
			}
			cout<<max1<<endl;
		}

	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pii;
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define endl "\n"
ll a[200005];
ll sum[200005]={};
int main()
{
	ios;
	ll t,n,p,k,f,ptr,d,diff,i,max1,z;
	cin>>t;
	for(z=1;z<=t;z++)
	{
		max1=0;f=0;
		cin>>n>>p>>k;
		for(i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+n+1);
		for(i=1;i<=n;i++)
			sum[i]=sum[i-1]+a[i];
		ptr=0;
		while(p>0&&(ptr+k)<=n)
		{
			d=a[ptr+k];
			if(d<=p)
			{
				ptr+=k;
				p-=d;
				f=1;
			}
			else
				break;
		}
		//cout<<"ptr = "<<ptr<<endl;
		if(f!=1)
		{
			while(p>0&&ptr<n)
			{
				d=a[ptr+1];
				if(d<=p)
				{
					ptr++;
					p-=d;
				}
				else
					break;
			}
			cout<<ptr<<endl;
		}
		else
		{
			max1=ptr;
			for(i=min(ptr+k-1,n);i>=ptr+1;i--)
			{
				ll q=i-ptr;
				ll res=p+a[k]-a[i];
				if(res>0)
				{
					d=min((ll)(lower_bound(sum+1,sum+q+1,res)-sum),(ll)(q));
					//cout<<d<<endl;
					max1=max(ptr+d,max1);
				}
			}
			cout<<max1<<endl;
		}
		if(t==10000&&z==74)
		{
			cout<<n<<" "<<k<<" "<<p<<endl;
			for(i=1;i<=n;i++)
				cout<<a[i]<<" ";
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pii;
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define endl "\n"
ll a[200005];
ll sum[200005]={};
int main()
{
	ios;
	ll t,n,p,k,f,ptr,d,diff,i,max1;
	cin>>t;
	while(t--)
	{
		max1=0;f=0;
		cin>>n>>p>>k;
		for(i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+n+1);
		for(i=1;i<=n;i++)
			sum[i]=sum[i-1]+a[i];
		ptr=0;
		while(p>0&&(ptr+k)<=n)
		{
			d=a[ptr+k];
			if(d<=p)
			{
				ptr+=k;
				p-=d;
				f=1;
			}
			else
				break;
		}
		//cout<<"ptr = "<<ptr<<endl;
		if(f!=1)
		{
			while(p>0&&ptr<n)
			{
				d=a[ptr+1];
				if(d<=p)
				{
					ptr++;
					p-=d;
				}
				else
					break;
			}
			cout<<ptr<<endl;
		}
		else
		{
			max1=ptr;
			for(i=min(ptr+k-1,n);i>=ptr+1;i--)
			{
				ll q=i-ptr;
				ll res=p+a[k]-a[i];
				if(res>0)
				{
					d=min((ll)(lower_bound(sum+1,sum+q+1,res)-sum),(ll)(q));
					//cout<<d<<endl;
					max1=max(ptr+d,max1);
				}
			}
			cout<<max1<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> pii;
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod 1000000007
#define endl "\n"
ll a[200005];
ll sum[200005]={};
int main()
{
	ios;
	ll t,n,p,k,f,ptr,d,diff,i,max1;
	cin>>t;
	while(t--)
	{
		max1=0;f=0;
		cin>>n>>p>>k;
		for(i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+n+1);
		for(i=1;i<=n;i++)
			sum[i]=sum[i-1]+a[i];
		ptr=0;
		while(p>0&&(ptr+k)<=n)
		{
			d=a[ptr+k];
			if(d<=p)
			{
				ptr+=k;
				p-=d;
				f=1;
			}
			else
				break;
		}
		//cout<<"ptr = "<<ptr<<endl;
		if(f!=1)
		{
			while(p>0&&ptr<n)
			{
				d=a[ptr+1];
				if(d<=p)
				{
					ptr++;
					p-=d;
				}
				else
					break;
			}
			cout<<ptr<<endl;
		}
		else
		{
			max1=ptr;
			ll q=1;
			for(i=min(ptr+k-1,n);i>=ptr+1;i--)
			{
				ll res=p+a[k]-a[i];
				if(res>0)
				{
					d=min(lower_bound(sum+1,sum+(k-q+1),res)-sum,k-1);
					//cout<<d<<endl;
					max1=max(ptr+d,max1);
				}
				q++;
			}
			cout<<max1<<endl;
		}
	}
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
ll n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll a[200001];

ll dp[200005];
ll func(ll index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	ll lloud=min(x-1,index+k-1);

	return min(p+1,min(a[index]+func(index+1),a[lloud]+func(loud+1)));
}
ll check(ll sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(ll i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

ll bin(ll beg,ll end)
{
	ll mid;
	while(beg<end)
	{
		 mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	ll i,j,fl,test;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld%lld%lld",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);

		printf("%lld\n",bin(0,n) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
ll n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll a[200001];

ll dp[200005];
ll func(ll index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	ll lloud=min(x-1,index+k-1);

	return min(p+1,min(a[index]+func(index+1),a[lloud]+func(lloud+1)));
}
ll check(ll sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(ll i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

ll bin(ll beg,ll end)
{
	ll mid;
	while(beg<end)
	{
		 mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	ll i,j,fl,test;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld%lld%lld",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);

		printf("%lld\n",bin(0,n) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
ll n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll a[200001];

ll dp[200005];
ll func(ll index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	ll lloud=min(x-1,index+k-1);
	if(index+k-1>=x)
		return dp[index]=a[index]+func(index+1);
	return dp[index]=min(p+1,min(a[index]+func(index+1),a[lloud]+func(lloud+1)));
}
ll check(ll sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(ll i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

ll bin(ll beg,ll end)
{
	ll mid;
	while(beg<end)
	{
		 mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	ll i,j,fl,test;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld%lld%lld",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);

		printf("%lld\n",bin(0,n) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
ll n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll a[200001];

ll dp[200005];
ll func(ll index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	ll lloud=min(x-1,index+k-1);
	if(index+k-1>=n)
		return dp[index]=a[index]+func(index+1);
	return dp[index]=min(p+1,min(a[index]+func(index+1),a[lloud]+func(lloud+1)));
}
ll check(ll sz)
{
	if(sz==1	)
		return 1;
	x=sz;
	ll i;
	for( i=0;i<=n;i++)
	{
		dp[i]=p+1;
	}
	dp[0]=a[0];
	int ans=a[0]<=p?0:-1;
	for(i=1;i<n;i++)
	{

		dp[i]=(dp[i-1]+a[i]);
		if(i-k>=0)
			dp[i]=min(dp[i],dp[i-k]+a[i]);
		else if(i-k==-1)
			dp[i]=min(dp[i],a[i]);
		if(dp[i]<=p)
			ans=i;
	}
	return ans+1;
}

ll bin(ll beg,ll end)
{
	ll mid;
	while(beg<end)
	{
		 mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	ll i,j,fl,test;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld%lld%lld",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);

		printf("%lld\n",check(0) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
ll n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

ll a[200001];

ll dp[200005];
ll func(ll index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	ll lloud=min(n-1,index+k-1);
	return min(p+1,min(a[index]+func(index+1),a[lloud]+func(index+k)));
}
ll check(ll sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(ll i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

ll bin(ll beg,ll end)
{
	ll mid;
	while(beg<end)
	{
		 mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	ll i,j,fl,test;
	scanf("%lld",&test);
	while(test--)
	{
		scanf("%lld%lld%lld",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%lld",&a[i]);
		}
		sort(a,a+n);

		printf("%lld\n",bin(0,n) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
int n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

int a[200001];

int dp[200005];
int func(int index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	int lloud=min(n-1,index+k-1);
	return min(a[index]+func(index+1),a[lloud]+func(index+k));
}
int check(int sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(int i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

int bin(int beg,int end)
{
	int mid;
	while(beg<end)
	{
		int mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	int i,j,fl,test;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d%d%d",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);

		printf("%d\n",bin(0,n) );
	}
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
ll mod =1000000007;
using namespace std;
int n,p,k,x;

ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}

int a[200001];

int dp[200005];
int func(int index)
{
	if(x<=index)
		return 0;
	if(dp[index]!=-1)
		return dp[index];

	int lloud=min(n-1,index+k-1);
	return min(p+1,min(a[index]+func(index+1),a[lloud]+func(index+k)));
}
int check(int sz)
{
	if(sz==0)
		return 1;
	x=sz;
	for(int i=0;i<sz;i++)
	{
		dp[i]=-1;
	}
	return func(0)<=p;
}

int bin(int beg,int end)
{
	int mid;
	while(beg<end)
	{
		int mid=(beg)+(end-beg+1)/2;
		if(check(mid))
		{
			beg=mid;
		}
		else
			end=mid-1;
	}
	return beg;
}
int main()
{
	int i,j,fl,test;
	scanf("%d",&test);
	while(test--)
	{
		scanf("%d%d%d",&n,&p,&k);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		sort(a,a+n);

		printf("%d\n",bin(0,n) );
	}
	return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

    for(int i = 1; i <= k; i++) {
        dp[i] = dp[i-1] + a[i-1];
    }
    vector<ll> pref = {0};
    for(int i = 0; i < n; i++) {
        pref.pb(pref.back() + a[i]);
    }
    for(int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1] + a[i-1], dp[i-k] + a[i-1]);
    }

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

ll log_k(ll x, ll k) {

}

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

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

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

ll log_k(ll x, ll k) {

}

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

    for(int i = 1; i <= k; i++) {
        dp[i] = dp[i-1] + a[i-1];
    }
    for(int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1] + a[i-1], dp[i-k] + a[i-1]);
    }

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

ll log_k(ll x, ll k) {

}

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

    for(int i = 1; i <= k; i++) {
        dp[i] = dp[i-1] + a[i-1];
    }
    vector<ll> pref = {0};
    for(int i = 0; i < n; i++) {
        pref.pb(pref.back() + a[i]);
    }
    for(int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1] + a[i-1], pref[i-k] + a[i-1]);
    }

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

    for(int i = 1; i <= k; i++) {
        dp[i] = dp[i-1] + a[i-1];
    }
    vector<ll> pref = {0};
    for(int i = 0; i < n; i++) {
        pref.pb(pref.back() + a[i]);
    }
    for(int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1] + a[i-1], dp[i-k] + a[i-1]);
    }

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

ll log_k(ll x, ll k) {

}

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

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

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

    cout << ans << endl;
}

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

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

    return 0;
}


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

#define pii  pair<int, int>
#define vi vector<int >
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end() //Forward traversal
#define rall(x) (x).rbegin, (x).rend() //reverse traversal

#define endl "\n"
#define ll long long
#define ld long double

const ld EPS = 10e-9;
const int INF = 1e9 + 5;
const long long INFLL = (ll)INF * (ll)INF;
const ll MOD = 1e9 + 7;
const ld PI = 3.14159265358979323846;

struct HASH{
    size_t operator()(const pair<int,int>&x)const{
        return hash<long long>()(((long long)x.first)^(((long long)x.second)<<32));
    }
};

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "[";
    for (int i = 0; i < (int)v.size(); ++i) {
        os << v[i];
        if (i != (int)v.size() - 1)
            os << ", ";
    }
    os << "]\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T>
ostream& operator<<(ostream& os, const unordered_set<T>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it;
        if (it != *v.rbegin())
            os << ", ";
    }
    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const unordered_map<T, S>& v)
{
    os << "{";
    for (const auto& it : v) {
        os << it.first << " : "
           << it.second;

        if( it != *v.rbegin())
            os  << ", ";
    }

    os << "}\n";
    return os;
}

template <typename T, typename S>
ostream& operator<<(ostream& os, const pair<T, S>& v)
{
    os << "(";
    os << v.first << ", "
       << v.second << ")";
    os << "\n";
    return os;
}

#define DEBUG

#ifdef DEBUG
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cerr << name << " : " << arg1;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
    const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define debug(...)
#endif

// __gcd(x, y)

// idea behind binary search: there is monotonically increasing predicate and range [l, r]. Find
// x in range such that x is smallest value for which predicate holds true.
//            int lo = 1, hi = 1e9;
//            while(lo < hi)
//            {
//                ll mid = (lo+hi) / 2;
//                if(predicate(mid))
//                {
//                    hi = mid;
//                }
//                else
//                {
//                    lo = mid+1;
//                }
//            }
//            assert(lo == hi);

// OR: given monotonically decreasing predicate, find x in range such that x is largest value for
// which predicate holds true.

//int x = 0;
//for(int s = max_step; s >= 1; s /= 2)
//{
//    while(predicate(x+s)) x += s;
//}
// after this, x is the largest value for which predicate is true

// How to calculate mid element:
// floor((l+r) / 2) might overflow, use l + floor((r-l)/2)

//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l < r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// AND
//
//ll l = 0, r = s.size()-1;
//ll ans = -1;
//while(l <= r)
//{
//    ll mid = l + (r-l) / 2;
//    if(P(mid)) {
//        r = mid-1;
//        ans = mid;
//    }
//    else
//    {
//        l = mid + 1;
//    }
//}
//
// ARE EQUIVALENT

#define ull unsigned long long

ll log_k(ll x, ll k) {

}

void solve() {
    ll n, p, k;
    cin >> n >> p >> k;

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

    sort(all(a));
    vector<ll> dp(n+1, INT_MAX);
    dp[0] = 0;

    for(int i = 1; i <= k; i++) {
        dp[i] = dp[i-1] + a[i-1];
    }
    for(int i = 2; i <= n; i++) {
        dp[i] = min(dp[i-1] + a[i-1], dp[i-k] + a[i-1]);
    }

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

    cout << ans << endl;
}

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

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

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long ll;

int main()
{
	ll t;
	cin>>t;
	while(t--)
	{
		ll n,p,k,a[300000],i,j,c=0,d=0,l,u;
		cin>>n>>p>>k;
		u=p;
		for(i=0;i<n;i++)
		{
			cin>>a[i];
		}
		sort(a,a+n);

		for(i=k-1;i<n;i+=k)
		{
			if(a[i]<=p)
			{
				c+=k;
				p-=a[i];
			}
			else
				break;
//			cout<<c<<" ";

		}
		if(i<n)
		{
			for(j=i-k+1;j<i;j++)
			{
				if(a[j]<=p)
				{
					c+=1;
					p-=a[j];
				}
			}
		}
		else
		{
			for(j=i-k+1;j<i&&j<n;j++)
			{
				if(a[j]<=p)
				{
					c+=1;
					p-=a[j];
				}
			}

		}
	//	cout<<endl;
		d=c;
		c=0;
		p=u;
		long pre=0,s=0;
		for(l=0;l<=k;l++)
		{
			s=pre;
			if(s>p)
				break;
			p=u;
			c=l;

			for(i=l+k-1;i<n;i+=k)
			{

				if(s+a[i]<=p)
				{
					c+=k;
					s+=a[i];
				}
				else
					break;
//				cout<<c<<" ";

			}
			pre+=a[l];

			 d= c>d?c:d;
		//	 cout<<c<<" ";

		}

		 cout<<d<<endl;

	}
  return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN];
int s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	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+n+1);
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		int pp=p;
		int ans=0, cnt;
		for (int i=1; i<k; i++) {
			cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		cnt=0;
		pp=p;
		for (int i=k; i<=n; i+=k) if (pp>=a[i]) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
#define NN 200010
int T, n, p, k;
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>T;
	while (T--) {
		cin>>n>>p>>k;
		int pp=p;
		for (int i=1; i<=n; i++) cin>>a[i];
		sort(a+1, a+n+1);
		int ans=0;
		int flag=0;
		for (int i=1; i<k; i++) {
			int sum=0;
			for (int j=i+k; j<=n; j+=k) {
				if (!p) break;
				if (p>=a[j]) p-=a[j], sum+=k;
			}
			if (p) {
				for (int ii=1; ii<k; ii++) {
					if (!p) break;
					if (p>=a[ii]) p-=a[ii], sum++;
				}
			}
			ans=max(ans, sum);
		}
		int sum=0;
		for (int i=k; i<=n; i+=k) {
			if (!pp) break;
			if (pp>=a[i]) pp-=a[i], sum+=k;
		}
		cout<<max(ans, sum)<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN], s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	for (scanf("%d", &T); T--;) {
		scanf("%d%d%d", &n, &p, &k);
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		int pp, ans=0;
		pp=p;
		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) if (a[j]<=pp) pp-=a[j], cnt+=k;
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) if (a[i]<=pp) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN], s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	for (scanf("%d", &T); T--;) {
		scanf("%d%d%d", &n, &p, &k);
		int pp=p, ans=0;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		if (n<=k) {
			if (n==k and a[n]<=pp) {printf("%d\n", k); continue;}
			for (int i=1; i<=n; i++) if (a[i]<=pp) pp-=a[i], ans++;
			printf("%d\n", ans); continue;
		}
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		pp=p;
		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) if (a[j]<=pp) pp-=a[j], cnt+=k;
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) if (a[i]<=pp) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
#define NN 200010
int T, n, p, k;
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>T;
	while (T--) {
		scanf("%d%d%d", &n, &p, &k);
		int pp;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		int ans=0;

		if (n<k) {
			for (int i=1; i<=n; i++) if (p>=a[i]) p-=a[i], ans++;
			cout<<ans<<endl; continue;
		}

		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			for (int j=1; j<=i; j++) {
				if (pp>=a[j]) pp-=a[j], cnt++;
			}
			ans=max(ans, cnt);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) {
			if (pp>=a[i]) pp-=a[i], cnt+=k;
		}
		cout<<max(ans, cnt)<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

int c[NN],mx[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,p,k;
		scanf("%d %d %d",&n,&p,&k);
		for(int i=1;i<=n;i++)scanf("%d",&c[i]);
		sort(c+1,c+n+1);
		mx[1]=c[1];
		for(int i=2;i<=n;i++){
			if(i<k)mx[i]=mx[i-1]+c[i];
			else mx[i]=c[i]+mx[i-k];
		}
		int id=0;
		for(int i=n;i>=1;i--){
			if(p>=mx[i]){
				id=i;
				break;
			}
		}
		printf("%d\n",id);
	}

	return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
#define NN 200010
int T, n, p, k;
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>T;
	while (T--) {
		scanf("%d%d%d", &n, &p, &k);
		int pp;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		int ans=0;

		if (n<k) {
			for (int i=1; i<=n; i++) if (p>=a[i]) p-=a[i], ans++;
			printf("%d\n", ans);  continue;
		}

		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			for (int j=1; j<=i; j++) {
				if (pp>=a[j]) pp-=a[j], cnt++;
			}
			ans=max(ans, cnt);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) {
			if (pp>=a[i]) pp-=a[i], cnt+=k;
		}
		printf("%d\n", max(ans, cnt));
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int a[200200],s[200200],sum[200200];
int main(){
	int t,n,p,k;
	scanf("%d",&t);
	while(t--){
		memset(a,0,sizeof a);
		memset(s,0,sizeof s);
		memset(sum,0,sizeof sum);

		scanf("%d%d%d",&n,&p,&k);
		for(int i=1;i<=n;i++) scanf("%d",a+i);
		sort(a+1,a+n+1);
		for(int i=k;i<=n;i++){
			s[i]=s[i-k]+a[i];
		}
		for(int i=1;i<k;i++) sum[i]=sum[i-1]+a[i];
		int it=upper_bound(s+1,s+n+1,p)-s-1;
		int ans=it/k,ss=ans*k;
		for(int i=ans*k+1;i<=it;i++){
			int pp=p-s[i];
			int d=upper_bound(sum+1,sum+(i%k)+1,pp)-sum-1;
			ss=max(ss,ans*k+d);
		}
		cout<<ss<<endl;
	}
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN], s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	for (scanf("%d", &T); T--;) {
		scanf("%d%d%d", &n, &p, &k);
		int pp, ans=0;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		if (n<k) {
			for (int i=1; i<=n; i++) if (a[i]<=p) p-=a[i], ans++;
			printf("%d\n", ans); continue;
		}
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		pp=p;
		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) if (a[j]<=pp) pp-=a[j], cnt+=k;
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) if (a[i]<=pp) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

#define NN 200010
int T, n, p, k;
int a[NN];
long long s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	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+n+1);

		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		int pp=p;
		int ans=0, cnt;
		for (int i=1; i<k; i++) {
			cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			pos = min(pos, i);
			ans=max(ans, cnt+pos);
		}
		cnt=0;
		pp=p;
		for (int i=k; i<=n; i+=k) if (pp>=a[i]) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include <bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
typedef double DO;
typedef long long INT;
typedef pair<int,int> pii;
typedef pair<pii,int> pi3;
template<typename T, typename U> inline void smin(T &a, const U &b) {if(a>b) a=b;}
template<typename T, typename U> inline void smax(T &a, const U &b) {if(a<b) a=b;}
#define NN 200010
int T, n, p, k;
int a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	cin>>T;
	while (T--) {
		cin>>n>>p>>k;
		int pp;
		for (int i=1; i<=n; i++) cin>>a[i];
		sort(a+1, a+n+1);
		int ans=0;

		if (n<k) {
			for (int i=1; i<=n; i++) if (p>=a[i]) p-=a[i], ans++;
			cout<<ans<<endl; continue;
		}

		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			for (int j=1; j<=i; j++) {
				if (pp>=a[j]) pp-=a[j], cnt++;
			}
			ans=max(ans, cnt);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) {
			if (pp>=a[i]) pp-=a[i], cnt+=k;
		}
		cout<<max(ans, cnt)<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
#define NN 200100
#define inf 1e9
using namespace std;

typedef long long INT;
typedef pair<int, int> pii;

int cs[NN];
int sum[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int test;
	scanf("%d", &test);
	while(test--){
		int n, p, k;
		scanf("%d%d%d", &n, &p, &k);
		for(int i = 1; i <= n; i++) scanf("%d", &cs[i]);
		sort(cs + 1, cs + n + 1);
		sum[1] = cs[1];
		for(int i = 2; i <= n; i++) sum[i] = sum[i - 1] + cs[i];
		int ans = 0;
		for(int i = 1; i <= k; i++){
			if(sum[i] > p) break;
			int temp = p;
			int cnt = i - 1;
			temp -= sum[i - 1];
			for(int j = i + k - 1; j <= n; j += k){
				if(temp >= cs[j]) {
					temp -= cs[j];
					cnt += k;
				}
				else {
					int tmp = upper_bound(sum + 1, sum + n + 1, sum[j - k] + temp) - sum - (j - k) - 1;
					cnt += tmp;
					break;
				}
			}
			ans = max(cnt, ans);
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

typedef long long INT;
typedef pair<int,int> pii;

#define NN 200010
int T, n, p, k;
int a[NN];
INT s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	for (scanf("%d", &T); T--;) {
		scanf("%d%d%d", &n, &p, &k);
		int pp=p, ans=0;
		for (int i=1; i<=n; i++) scanf("%d", &a[i]);
		sort(a+1, a+n+1);
		if (n<=k) {
			if (n==k and a[n]<=pp) {printf("%d\n", k); continue;}
			for (int i=1; i<=n; i++) if (a[i]<=pp) pp-=a[i], ans++;
			printf("%d\n", ans); continue;
		}
		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		pp=p;
		for (int i=1; i<k; i++) {
			int cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) if (a[j]<=pp) pp-=a[j], cnt+=k;
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		pp=p;
		int cnt=0;
		for (int i=k; i<=n; i+=k) if (a[i]<=pp) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


#include<bits/stdc++.h>

#define NN 200100

using namespace std;

typedef long long INT;

int c[NN],mx[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int t;
	cin>>t;
	while(t--){
		int n,p,k;
		scanf("%d %d %d",&n,&p,&k);
		for(int i=1;i<=n;i++)scanf("%d",&c[i]);
		sort(c+1,c+n+1);
		mx[1]=c[1];
		for(int i=2;i<=n;i++){
			if(i<k)mx[i]=mx[i-1]+c[i];
			else mx[i]=c[i]+mx[i-k];
		}
		int id=upper_bound(mx+1,mx+n+1,p)-mx-1;
		printf("%d\n",id);
	}

	return 0;
}


#include <bits/stdc++.h>

#define x first
#define y second
using namespace std;

#define NN 200010
int T, n, p, k;
int a[NN];
long long s[NN];

int main () {
#ifndef ONLINE_JUDGE
   freopen("in.in","r",stdin);
   freopen("out.out", "w", stdout);
#endif
	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+n+1);

		for (int i=1; i<k; i++) s[i]=s[i-1]+a[i];
		int pp=p;
		int ans=0, cnt;
		for (int i=1; i<k; i++) {
			cnt=0;
			pp=p;
			for (int j=i+k; j<=n; j+=k) {
				if (pp>=a[j]) pp-=a[j], cnt+=k;
			}
			int pos=upper_bound(s+1, s+k, pp)-s-1;
			ans=max(ans, cnt+pos);
		}
		cnt=0;
		pp=p;
		for (int i=k; i<=n; i+=k) if (pp>=a[i]) pp-=a[i], cnt+=k;
		printf("%d\n", max(ans, cnt));
	}
}


/*
ID: grandea2
PROG: transform
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int>par;
typedef vector<par>vpar;
#define forn(i,n) for(int i=0;i<n;i++)
#define get(n) ll (n); cin>>(n);
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back
#define mp make_pair
const ll inf = 1e18L+1;
const ll mod=1e9+7;
double pie=3.1415926535897;
#define outa(a,e) forn(i,e) cout << a[i] << (i==e-1?'\n':' ')
#define oute(n) cout<<n<<"\n"

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    /*#ifndef ONLINE_JUDGE
    freopen("transform.in", "r", stdin);
    freopen("transform.out", "w", stdout);
    #endif
    */
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w",stderr);
    #endif
    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 sum=0,cnt=0,ans=0,start=0;
        for(int i=0;i<n;i++){
            sum=start;
            cnt=i;
            if(sum>p)break;
            for(int j=i+k-1;j<n;j+=k){
                if(sum+a[j]>p)break;
                sum+=a[j];
                cnt+=k;
            }
            ans=max(ans,cnt);
            start+=a[i];
        }
        cout<<ans<<"\n";
    }
return 0;
}


/*
ID: grandea2
PROG: transform
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int>par;
typedef vector<par>vpar;
#define forn(i,n) for(int i=0;i<n;i++)
#define get(n) ll (n); cin>>(n);
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back
#define mp make_pair
const ll inf = 1e18L+1;
const ll mod=1e9+7;
double pie=3.1415926535897;
#define outa(a,e) forn(i,e) cout << a[i] << (i==e-1?'\n':' ')
#define oute(n) cout<<n<<"\n"

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    /*#ifndef ONLINE_JUDGE
    freopen("transform.in", "r", stdin);
    freopen("transform.out", "w", stdout);
    #endif
    */
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w",stderr);
    #endif
    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 ans=0,start=0;
        for(int i=0;i<n;i++){
            int sum=start;
            int cnt=i;
            if(sum>p)break;
            for(int j=i+k-1;j<n;j+=k){
                if(sum+a[j]<=p){
                    sum+=a[j];
                    cnt+=k;
                }else break;
            }
            ans=max(ans,cnt);
            start+=a[i];
        }
        cout<<ans<<"\n";
    }
return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    for (int test = 0; test < t; test++) {
        int n, p, k;
        cin >> n >> p >> k;
        int start = 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 = start;
            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;
                }
            }
            start += a[i];
            ans = max(ans, cnt);
        }
        cout << ans << "\n";
    }
}


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

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int t;
    cin>>t;
    for(int z=0;z<t;z++){
        int n,p,k;
        cin>>n>>p>>k;
        vector<long long>a(n);
        for(int i=0;i<n;i++)cin>>a[i];
        sort(a.begin(),a.end());
        int ans=0,start=0;
        for(int i=0;i<n;i++){
            int sum=start;
            int cnt=i;
            if(sum>p)break;
            for(int j=i+k-1;j<n;j+=k){
                if(sum+a[j]<=p){
                    sum+=a[j];
                    cnt+=k;
                }else break;
            }
            ans=max(ans,cnt);
            start+=a[i];
        }
        cout<<ans<<"\n";
    }
return 0;
}


/*
ID: grandea2
PROG: transform
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int>par;
typedef vector<par>vpar;
#define forn(i,n) for(int i=0;i<n;i++)
#define get(n) ll (n); cin>>(n);
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back
#define mp make_pair
const ll inf = 1e18L+1;
const ll mod=1e9+7;
double pie=3.1415926535897;
#define outa(a,e) forn(i,e) cout << a[i] << (i==e-1?'\n':' ')
#define oute(n) cout<<n<<"\n"

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    /*#ifndef ONLINE_JUDGE
    freopen("transform.in", "r", stdin);
    freopen("transform.out", "w", stdout);
    #endif
    */
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w",stderr);
    #endif
    long long t;
    cin>>t;
    while(t--){
        long long n,p,k;
        cin>>n>>p>>k;
        vector<ll>a(n);
        for(long long i=0;i<n;i++)cin>>a[i];
        sort(a.begin(),a.end());
        long long sum=0,cnt=0,ans=0,start=0;
        for(long long i=0;i<n;i++){
            sum=start;
            cnt=i;
            if(sum>p)break;
            for(long long j=i+k-1;j<n;j+=k){
                if(sum+a[j]>p)break;
                sum+=a[j];
                cnt+=k;
            }
            ans=max(ans,cnt);
            start+=a[i];
        }
        cout<<ans<<endl;
    }
return 0;
}


/*
ID: grandea2
PROG: transform
LANG: C++
*/
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
typedef vector<int> vec;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int>par;
typedef vector<par>vpar;
#define forn(i,n) for(int i=0;i<n;i++)
#define get(n) ll (n); cin>>(n);
#define setmax(a,b) (a)=max((a),(b))
#define setmin(a,b) (a)=min((a),(b))
#define pb push_back
#define mp make_pair
const ll inf = 1e18L+1;
const ll mod=1e9+7;
double pie=3.1415926535897;
#define outa(a,e) forn(i,e) cout << a[i] << (i==e-1?'\n':' ')
#define oute(n) cout<<n<<"\n"

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    /*#ifndef ONLINE_JUDGE
    freopen("transform.in", "r", stdin);
    freopen("transform.out", "w", stdout);
    #endif
    */
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w",stderr);
    #endif
    int t;
    cin>>t;
    forn(i,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 ans=0,start=0;
        for(int i=0;i<n;i++){
            int sum=start;
            int cnt=i;
            if(sum>p)break;
            for(int j=i+k-1;j<n;j+=k){
                if(sum+a[j]<=p){
                    sum+=a[j];
                    cnt+=k;
                }else break;
            }
            ans=max(ans,cnt);
            start+=a[i];
        }
        cout<<ans<<"\n";
    }
return 0;
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(p-a[j]>=0){
						s1++;
						p=p-a[j];
					}
					else break;
				}
				break;
			}
			if(p-a[i+k-1]>=0){
				p=p-a[i+k-1];
				s1=s1+k;
			}
		}
		s2=0;
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(pp-a[j]>=0){
						s2++;
						pp=pp-a[j];
					}
					else break;
				}
				break;
			}
			if(pp-a[i+k-1]>=0){
				pp=pp-a[i+k-1];
				s2=s2+k;
			}
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) break;
			if(p-a[i+k-1]>=0){
				p=p-a[i+k+1];
				s1=s1+k;
			}
		}
		s2=0;
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(pp-a[j]>=0){
						s2++;
						pp=pp-a[j];
					}
					else break;
				}
				break;
			}
			if(pp-a[i+k-1]>=0){
				pp=pp-a[i+k+1];
				s2=s2+k;
			}
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,sum,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		sum=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) break;
			if(p-a[i+k-1]>=0){
				p=p-a[i+1];
				s1=s1+k;
			}
		}
		s2=0;
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(pp-a[j]>=0){
						s2++;
						pp=pp-a[j];
					}
					else break;
				}
				break;
			}
			if(pp-a[i+k-1]>=0){
				pp=pp-a[i+1];
				s2=s2+k;
			}
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,sum,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		sum=0;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			sum+=a[i];
		}
		if(sum<=p) cout<<n<<endl;
		else {
			sort(a+1,a+1+n);
			s1=0;
			for(int i=1;i<=n;i+=k){
				if(i+k-1>n) break;
				if(p-a[i+k-1]>=0){
					p=p-a[i+1];
					s1=s1+k;
				}
			}
			s2=0;
			for(int i=1;i<=k-1;i++){
				if(pp-a[i]>=0){
					s2++;
					pp=pp-a[i];
				}
				else break;
			}
			for(int i=k;i<=n;i+=k){
				if(i+k-1>n) {
					for(int j=i;j<=n;j++){
						if(pp-a[j]>=0){
							s2++;
							pp=pp-a[j];
						}
						else break;
					}
					break;
				}
				if(pp-a[i+k-1]>=0){
					pp=pp-a[i+1];
					s2=s2+k;
				}
			}
			cout<<max(s2,s1)<<endl;
		}
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		for(int i=1;i<=n;i++)
			cin>>a[i];
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) break;
			if(p-a[i+k-1]>=0){
				//cout<<a[i+1]<<endl;
				p=p-a[i+1];
				s1=s1+k;
			}
		}
		//cout<<endl;
		s2=0;
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				//cout<<a[i]<<endl;
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
//		cout<<pp<<" "<<s2<<endl;
//		cout<<endl;
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) break;
			if(pp-a[i+k-1]>=0){
				//cout<<a[i+1]<<endl;
				pp=pp-a[i+1];
				s2=s2+k;
			}
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,k,s;
long long p,a[200005],sum[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		sum[0]=0;
		s=0;
		for(int i=1;i<=k-1;i++) sum[i]=sum[i-1]+a[i];
		sum[k]=a[k];
		for(int i=k+1;i<=n;i++) sum[i]=sum[i-k]+a[i];
		for(int i=1;i<=n;i++)
			if(sum[i]<=p) s=i;

		cout<<s<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp,ppp;
long long s1,s2,s3,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(p-a[j]>=0){
						s1++;
						p=p-a[j];
					}
					else break;
				}
				break;
			}
			if(p-a[i+k-1]>=0){
				p=p-a[i+k-1];
				s1=s1+k;
			}
		}
		s2=0;
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(pp-a[j]>=0){
						s2++;
						pp=pp-a[j];
					}
					else break;
				}
				break;
			}
			if(pp-a[i+k-1]>=0){
				pp=pp-a[i+k-1];
				s2=s2+k;
			}
		}
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
int t,n,p,k,pp;
long long s1,s2,a[200005];

int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		pp=p;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		s1=0;
		for(int i=1;i<=n;i+=k){
			if(i+k-1>n) break;
			if(p-a[i+k-1]>=0){
				p=p-a[i+k-1];
				s1=s1+k;
			}
		}
		s2=0;
		for(int i=1;i<=k-1;i++){
			if(pp-a[i]>=0){
				s2++;
				pp=pp-a[i];
			}
			else break;
		}
		for(int i=k;i<=n;i+=k){
			if(i+k-1>n) {
				for(int j=i;j<=n;j++){
					if(pp-a[j]>=0){
						s2++;
						pp=pp-a[j];
					}
					else break;
				}
				break;
			}
			if(pp-a[i+k-1]>=0){
				pp=pp-a[i+k-1];
				s2=s2+k;
			}
		}
		cout<<max(s2,s1)<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	v.push_back(0);
    	for(i=1;i<=n;i++){
    	    cin>>x;
    	    v.push_back(x);
    	   // v[i] += v[max((i-k),0LL)];
    	}
    	sort(v.begin(),v.end());
    	for(i=1;i<=n;i++)
    	v[i] += v[max((i-k),0LL)];
    	auto it = lower_bound(v.begin(),v.end(),p);
    	ll diff = it - v.begin();
    	diff--;
    	ans = diff;
    	if(it!=v.end() and *it == p){
    	    auto it1=v.begin();
    	    for(it1=it;it1!=v.end();it1++){
    	        if(*it1>p){
    	           // it = it1;
    	           // ans = it - v.begin();
    	           // ans--;
    	            break;
    	        }
    	    }
    	    it = it1;
    	    diff = it-v.begin();
    	    diff--;
    	    ans=diff;
    	}
    	ll mode = diff%k;
    	it--;
    	p-=*it;
    	for(i=1;i<mode;i++){
    	    if(v[i]>p){
    	        ans -= (mode-i);
    	        break;
    	    }
    	    p-=v[i];
    	}
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	v.push_back(0);
    	for(i=1;i<=n;i++){
    	    cin>>x;
    	    v.push_back(x);
    	}
    	sort(v.begin(),v.end());
    	for(i=1;i<=n;i++)
    	v[i] += v[max((i-k),0LL)];
    	auto it = lower_bound(v.begin(),v.end(),p);
    	ll diff = it - v.begin();
    	diff--;
    	ans = diff;
    	if(it != v.end() and *it == p){
    	    auto it1=v.begin();
    	    for(it1=it;it1!=v.end();it1++){
    	        if(*it1>p)
    	            break;
    	    }
    	    it = it1;
    	    diff = it-v.begin();
    	    diff--;
    	    ans=diff;
    	}
    	ll mode = diff%k;
    	it--;
    	p -= *it;
    	for(i=1;i<=mode;i++){
    	    if(v[i]>p){
    	        ans -= (mode-i);
    	        break;
    	    }
    	    p-=v[i];
    	}
    	cout<<ans<<"\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll inf=1000000000000000007;
ll mod=1000000007;
vector<ll> adj[100005];

int main()
{
	fio
	bool flag=true;
	ll test_case=1,tc;
	if(flag)
	cin>>test_case;
	tc=test_case;
	while(tc--){
    	ll n,p,k,i,j,x,sum = 0,ans = 0,c = 0,flag = 0;
    	ll maxi = -1e9,mini = 1e9;
    	vector <ll> v;
    	map<ll,ll>mp;
    	string s;
    	cin>>n>>p>>k;
    	v.push_back(0);
    	for(i=1;i<=n;i++){
    	    cin>>x;
    	    v.push_back(x);
    	   // v[i] += v[max((i-k),0LL)];
    	}
    	sort(v.begin(),v.end());
    	for(i=1;i<=n;i++)
    	v[i] += v[max((i-k),0LL)];
    	auto it = lower_bound(v.begin(),v.end(),p);
    	ll diff = it - v.begin();
    	diff--;
    	ans = diff;
    	if(*it == p){
    	    auto it1=v.begin();
    	    for(it1=it;it1!=v.end();it1++){
    	        if(*it1>p){
    	           // it = it1;
    	           // ans = it - v.begin();
    	           // ans--;
    	            break;
    	        }
    	    }
    	    it = it1;
    	    diff = it-v.begin();
    	    diff--;
    	    ans=diff;
    	}
    	ll mode = diff%k;
    	it--;
    	p-=*it;
    	for(i=1;i<mode;i++){
    	    if(v[i]>p){
    	        ans -= (mode-i);
    	        break;
    	    }
    	    p-=v[i];
    	}
    	cout<<ans<<"\n";
	}
	return 0;
}