This is the hard version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: 2 ≤ k ≤ n.

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "k of goods for the price of one" is held in store.

Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by a_i — the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

  * Vasya can buy one good with the index i if he currently has enough coins (i.e p ≥ a_i). After buying this good, the number of Vasya's coins will decrease by a_i, (i.e it becomes p := p - a_i). 
  * Vasya can buy a good with the index i, and also choose exactly k-1 goods, the price of which does not exceed a_i, if he currently has enough coins (i.e p ≥ a_i). Thus, he buys all these k goods, and his number of coins decreases by a_i (i.e it becomes p := p - a_i). 



Please note that each good can be bought no more than once.

For example, if the store now has n=5 goods worth a_1=2, a_2=4, a_3=3, a_4=5, a_5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

Input

The first line contains one integer t (1 ≤ t ≤ 10^4) — the number of test cases in the test.

The next lines contain a description of t test cases. 

The first line of each test case contains three integers n, p, k (2 ≤ n ≤ 2 ⋅ 10^5, 1 ≤ p ≤ 2⋅10^9, 2 ≤ k ≤ n) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains n integers a_i (1 ≤ a_i ≤ 10^4) — the prices of goods.

It is guaranteed that the sum of n for all test cases does not exceed 2 ⋅ 10^5.

Output

For each test case in a separate line print one integer m — the maximum number of goods that Vasya can buy.

Example

Input


8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2


Output


3
4
1
1
2
0
4
5
// package com.company;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.sql.SQLOutput;
import java.util.*;
public class Main {

    public static void main(String[] args) throws IOException {
        Scanner s = new Scanner(System.in);
        //BufferedReader s=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb1 = new StringBuilder();//int n=Integer.parseInt(s.readLine());
        int t=s.nextInt();
        while(t-->00){
            int n=s.nextInt();int p=s.nextInt();
            int k=s.nextInt();
            long[] sum=new long[n];
            int[] a=new int[n];
            for(int i=0;i<n;i++){
                a[i]=s.nextInt();
            }Arrays.sort(a);
            for(int i=0;i<n;i++){
                if(i==0) sum[i]=a[i];
                else{
                    sum[i]=sum[i-1]+a[i];
                }
            }long ans=Long.MIN_VALUE;
            for(int i=0;i<k;i++){int x=p;int count=0;
                if(i>0){
                    if(sum[i-1]<=x){
                        x-=sum[i-1];count=i;
                    }
                }
                for(int j=i+k-1;j<n;j+=k){
                    if(a[j]<=x){
                        count+=k;x-=a[j];
                    }else{
                        int low=j+1;int high=j+k;int ta=0;
                        while(low<=high&&high<n){
                            int mid=(low+high)/2;
                            if(sum[mid]-sum[j]<=x){
                                ta=mid-j;low=mid+1;
                            }else{
                                high=mid-1;
                            }
                        }
                        count+=ta;break;
                    }
                }ans=Math.max(ans,count);count=0;
            }sb1.append(ans+"\n");
        }
        System.out.println(sb1.toString());
    }



    static int ans1=Integer.MAX_VALUE;static int[] vis;
    static   int[] dp;static Queue<Integer>q;
    static void newdfs(int i1,char[][] a,int k,int n,int val){
        while(!q.isEmpty()){
            int i=q.poll();
            vis[i]=1;

            for(int j=i;j<=Math.min(n-1,i+k);j++){
                if(a[i][j]=='1'&&(vis[j]==0)&&dp[i]+1<dp[j]){
                    dp[j]=dp[i]+1;
                    q.add(j);
                }
            }for(int j=i;j>=Math.max(0,i-k);j--){
                if(a[i][j]=='1'&&(vis[j]==0)&&dp[j]+1<dp[i]){
                    dp[j]=dp[i]+1;
                    q.add(j);
                }
            }}
    }
    static  void computeLPSArray(String pat, int M, int lps[])
    {
        int len = 0;
        int i = 1;
        lps[0] = 0;
        while (i < M) {
            if (pat.charAt(i) == pat.charAt(len)) {
                len++;
                lps[i] = len;
                i++;
            }
            else // (pat[i] != pat[len])
            {
                if (len != 0) {
                    len = lps[len - 1];
                }
                else // if (len == 0)
                {
                    lps[i] = len;
                    i++;
                }
            }
        }
    }
    static boolean isPrime(int n)
    {
        // Corner cas
        if (n <= 1) return false;
        if (n <= 3) return true;

        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n % 2 == 0 || n % 3 == 0) return false;

        for (int i = 5; i * i <= n; i = i + 6)
            if (n % i == 0 || n % (i + 2) == 0)
                return false;

        return true;
    }
    static boolean isPrime(int n, int k)
    {
        // Corner cases
        if (n <= 1 || n == 4) return false;
        if (n <= 3) return true;

        // Try k times
        while (k > 0)
        {
            // Pick a random number in [2..n-2]
            // Above corner cases make sure that n > 4
            int a = 2 + (int)(Math.random() % (n - 4));

            // Fermat's little theorem
            if (power(a, n - 1, n) != 1)
                return false;

            k--;
        }

        return true;
    }
    static double power(double x, long y, int p) {
        double res = 1;
        x = x % p;
        while (y > 0) {
            if ((y & 1) == 1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    public static void fracion(double x) {
        String a = "" + x;
        String spilts[] = a.split("\\."); // split using decimal
        int b = spilts[1].length(); // find the decimal length
        int denominator = (int) Math.pow(10, b); // calculate the denominator
        int numerator = (int) (x * denominator); // calculate the nerumrator Ex
        // 1.2*10 = 12
        int gcd = (int)gcd((long)numerator, denominator); // Find the greatest common
        // divisor bw them
        String fraction = "" + numerator / gcd + "/" + denominator / gcd;
        // System.out.println((denominator/gcd));
        long x1=modInverse(denominator / gcd,998244353  );
        //  System.out.println(x1);
        System.out.println((((numerator / gcd )%998244353 *(x1%998244353  ))%998244353  ) );
    }static int max=Integer.MIN_VALUE;
    static int max1=Integer.MIN_VALUE;static int[][] c;static int k=1;static int bhoo=0;
    static int[] car;static int ans;static HashMap<Integer,Integer>[] ha;
    public static void dfs(int ans2,int papa, int i, ArrayList<Integer>[] h, int[] vis,HashMap<Integer,Integer>[] bad) {
        vis[i] = 1;
        if(i!=1&&h[i].size()==1){
            if(ans2!=-1)  {ans++;  sb1.append(ans2+" ");}
        }
        if(h[i]==null) return;
        int hu=0;int hai=0;
        for(Integer j:h[i]){
            if(vis[j]==0&&bad[i].containsKey(j)) {
                hai=1;break;
            }
        }int khi=0;
        for(Integer j:h[i]) {//if(vis[j]==0) flag=1;
            if(vis[j]==0){
                if(!bad[i].containsKey(j)){
                    if(hai==0&&khi==0){khi=1;    dfs(ans2,papa,j,h,vis,bad);}
                    else dfs(-1,papa,j,h,vis,bad);
                }else{
                    if(hu==0){
                        if(papa==0){//ans++;//sb1.append(j+" ");
                            dfs(j,1,j,h,vis,bad);
                        }else{
                            hu=1;
                            dfs( j,1,j,h,vis,bad);}
                    }else{
                        //    ans++;//sb1.append(j+" ");
                        dfs(j,1,j,h,vis,bad);
                    }
                }
            }
        }//if(flag==1) bhoo=-1;
    }static StringBuilder sb1=new StringBuilder();
    public static void bfs(int i, HashMap<Integer, Integer>[] h, int[] vis, int len, int papa) {
        Queue<Integer> q = new LinkedList<Integer>();
        q.add(i);
    }
    static int i(String a) {
        return Integer.parseInt(a);
    }
    static long l(String a) {
        return Long.parseLong(a);
    }
    static String[] inp() throws IOException {
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        return s.readLine().trim().split("\\s+");
    }


    static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    static long modInverse(int a, int m)
    {


        {
            // If a and m are relatively prime, then modulo inverse
            // is a^(m-2) mode m
            return (power(a, m - 2, m));
        }
    }

    // To compute x^y under modulo m
    static long power(int x, int y, int m)
    {
        if (y == 0)
            return 1;

        long p = power(x, y / 2, m) % m;
        p = (p * p) % m;

        if (y % 2 == 0)
            return p;
        else
            return (x * p) % m;
    }

    // Function to return gcd of a and b

}
class Student
{
    int l;int r;int i;
    public Student(int l, int r,int i) {
        this.l = l;
        this.r = r;this.i=i;
    }
    // Constructor


    // Used to print student details in main()
    public String toString()
    {
        return this.l+" ";
    }
}

class Sortbyroll implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b){
        return a.l-b.l;  }
} class Sortbyroll1 implements Comparator<Student>
{
    // Used for sorting in ascending order of
    // roll number
    public int compare(Student a, Student b){
        return a.l-b.l;
    }
}