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
/*Author: Sarthak Srivastava, BITS-Pilani*/
import java.io.*;
import java.util.*;
import java.text.*; 
import java.lang.*;
import java.math.*;
public class b{
/*********************************************Constants******************************************/
static PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));        
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static long mod=(long)1e9+7;
static long mod1=998244353;
static boolean sieve[];
static ArrayList<Integer> primes;
static long factorial[],invFactorial[];
static ArrayList<Integer> graph[];
static int pptr=0;
static String st[];
/****************************************Solutions Begins***************************************/
public static void main(String args[]) throws Exception{
    nl();
    int t=pi();
    while(t-->0){
        nl();
        int ob=pi();
        long coins=pl();
        int k=pi();
        Long objects[]=new Long[ob];
        nl();
        for(int i=0;i<ob;i++){
            objects[i]=pl();
        }
        
        
        Arrays.sort(objects);
        long[] dp = new long[ob+1];
        for(int i = 1; i<= ob; i++)
        {
            if(i>=k)
            {
                dp[i] = dp[i - k]  + objects[i-1];
            }
            else
            {
                dp[i] = dp[i - 1] + objects[i - 1];
            }
        }
        int ans = 0;
        for(int i = 0; i<=ob; i++)
        {
            if(dp[i]<=coins)
            ans = i;
        }
         
        out.println(ans);
    }
/****************************************Solutions Ends**************************************************/
    out.flush();
    out.close();
}
/****************************************Template Begins************************************************/
static void nl() throws Exception{
    pptr=0;
    st=br.readLine().split(" ");
}
static void nls() throws Exception{
    pptr=0;
    st=br.readLine().split("");
}
static int pi(){
    return Integer.parseInt(st[pptr++]);
}
static long pl(){
    return Long.parseLong(st[pptr++]);
}
static double pd(){
    return Double.parseDouble(st[pptr++]);
}
static String ps(){
    return st[pptr++];
}
/***************************************Precision Printing**********************************************/
static void printPrecision(double d){
    DecimalFormat ft = new DecimalFormat("0.000000000000"); 
    out.print(ft.format(d));
}
/**************************************Bit Manipulation**************************************************/
static void printMask(long mask){
    System.out.println(Long.toBinaryString(mask));
}
static int countBit(long mask){
    int ans=0;
    while(mask!=0){
        mask&=(mask-1);
        ans++;
    }
    return ans;
}
/******************************************Graph*********************************************************/
static void Makegraph(int n){
    graph=new ArrayList[n];
    for(int i=0;i<n;i++){
        graph[i]=new ArrayList<>();
    }
}
static void addEdge(int a,int b){
    graph[a].add(b);
}
// static void addEdge(int a,int b,int c){
//     graph[a].add(new Pair(b,c));
// }    
/*********************************************PAIR********************************************************/
static class Pair implements Comparable<Pair> {
    int u;
    int v;
    int index=-1;
    public Pair(int u, int v) {
        this.u = u;
        this.v = v;
    }
    public int hashCode() {
        int hu = (int) (u ^ (u >>> 32));
        int hv = (int) (v ^ (v >>> 32));
        return 31 * hu + hv;
    }
    public boolean equals(Object o) {
        Pair other = (Pair) o;
        return u == other.u && v == other.v;
    }
    public int compareTo(Pair other) {
        if(index!=other.index)
            return Long.compare(index, other.index);
        return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
    }
    public String toString() {
        return "[u=" + u + ", v=" + v + "]";
    }
}
/******************************************Long Pair*******************************************************/
static class Pairl implements Comparable<Pairl> {
        long u;
        long v;
        int index=-1;
        public Pairl(long u, long v) {
            this.u = u;
            this.v = v;
        }
        public int hashCode() {
            int hu = (int) (u ^ (u >>> 32));
            int hv = (int) (v ^ (v >>> 32));
            return 31 * hu + hv;
        }
        public boolean equals(Object o) {
            Pairl other = (Pairl) o;
            return u == other.u && v == other.v;
        }
        public int compareTo(Pairl other) {
            if(index!=other.index)
                return Long.compare(index, other.index);
            return Long.compare(v, other.v)!=0?Long.compare(v, other.v):Long.compare(u, other.u);
        }
        public String toString() {
            return "[u=" + u + ", v=" + v + "]";
        }
    }
/*****************************************DEBUG***********************************************************/
public static void debug(Object... o){
    System.out.println(Arrays.deepToString(o));
}
/************************************MODULAR EXPONENTIATION***********************************************/
static long modulo(long a,long b,long c){
    long x=1;
    long y=a%c;
    while(b > 0){
        if(b%2 == 1){
            x=(x*y)%c;
        }
        y = (y*y)%c; // squaring the base
        b /= 2;
    }
    return  x%c;
}
/********************************************GCD**********************************************************/
static long gcd(long x, long y){
    if(x==0)
        return y;
    if(y==0)
        return x;
    long r=0, a, b;
    a = (x > y) ? x : y; // a is greater number
    b = (x < y) ? x : y; // b is smaller number
    r = b;
    while(a % b != 0){
        r = a % b;
        a = b;
        b = r;
    }
    return r;
}
/******************************************SIEVE**********************************************************/
static void sieveMake(int n){
    sieve=new boolean[n];
    Arrays.fill(sieve,true);
    sieve[0]=false;
    sieve[1]=false;
    for(int i=2;i*i<n;i++){
        if(sieve[i]){
            for(int j=i*i;j<n;j+=i){
                sieve[j]=false;
            }
        }
    }
    primes=new ArrayList<Integer>();
    for(int i=0;i<n;i++){
        if(sieve[i]){
            primes.add(i);
        }
    }        
}
/********************************************End***********************************************************/
}