#include <iostream>
#include<bits/stdc++.h>

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define vi vector<int>
#define vl vector<ll>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define For(i, n) for(int i=0;i<n;i++)
using namespace std;

class p431C {
public:
    void solve() {
        ll n, k, l;
        cin >> n >> k >> l;
        vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));
        dp[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i && j <= k; j++) {
                if (j < l) {
                    dp[i][0] += dp[i - j][0];
                    dp[i][1] += dp[i - j][1];
                } else {
                    dp[i][1] += dp[i - j][1] + dp[i - j][0];
                }
                dp[i][0] %= mod;
                dp[i][1] %= mod;
            }
        }
        cout << dp[n][1] << endl;
    }
};

class p1336A {
public:
    vector<vi > e;
    vl res;

    int dfs(int cur, int fa, int dept) {
        int cnt = 1;
        for (int i:e[cur]) {
            if (i == fa) continue;
            cnt += dfs(i, cur, dept + 1);
        }
        res.push_back(dept - cnt);
        return cnt;
    }

    void solve() {
        int n, k;
        cin >> n >> k;
        e = vector<vi >(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        res.clear();
        dfs(1, 0, 1);
        sort(res.begin(), res.end());
        reverse(res.begin(), res.end());
        ll ans = 0;
        for (int i = 0; i < k; i++) {
            ans += res[i];
        }
        cout << ans << endl;
    }
};

class p706C {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        for (int i = 0; i < n; i++) cin >> a[i];
        vector<string> s(n);
        for (int i = 0; i < n; i++) cin >> s[i];
        vector<vector<ll>> dp(n, vector<ll>(2, -1));
        dp[0][0] = 0;//不翻转
        dp[0][1] = a[0];//翻转
        auto check = [](string s, string t, bool a, bool b) {
            if (a) reverse(s.begin(), s.end());
            if (b) reverse(t.begin(), t.end());
            for (int i = 0, j = 0; i < s.size() && j < t.size(); i++, j++) {
                if (s[i] < t[j]) return true;
                if (s[i] > t[j]) return false;
            }
            return s.size() <= t.size();
        };
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    if (dp[i - 1][j] != -1 && check(s[i - 1], s[i], j, k)) {
                        auto cur = dp[i - 1][j] + k * a[i];
                        dp[i][k] = dp[i][k] == -1 ? cur : min(cur, dp[i][k]);
                    }
                }
            }
        }
        if (dp[n - 1][0] == -1 && dp[n - 1][1] == -1) cout << -1 << endl;
        else {
            if (dp[n - 1][0] != -1 && dp[n - 1][1] != -1) cout << min(dp[n - 1][0], dp[n - 1][1]);
            else if (dp[n - 1][0] != -1) cout << dp[n - 1][0];
            else cout << dp[n - 1][1];
        }

    }
};

class p1373D {
public:
    void solve2() {
        //使用dp的方法，来进行求解，整个状态分为三种，未进行翻转，正在进行翻转，已经进行完了翻转
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vector<vl > dp(n + 1, vector<ll>(3, 0));
            for (int i = 1; i <= n; i++) {
                dp[i][0] = max(dp[i][0], dp[i - 1][0] + (i % 2 == 1 ? a[i - 1] : 0));
                if (i >= 2)
                    dp[i][1] = max(dp[i][1], max(dp[i - 2][0], dp[i - 2][1]) + (i % 2 == 1 ? a[i - 2] : a[i - 1]));
                dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][2]) + (i % 2 == 1 ? a[i - 1] : 0));
            }
            cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
        }
    }

    void solve() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vl v1, v2;
            auto check = [](vl &a) {
                if (a.size() == 0) return 0ll;
                ll sum = a.front(), msum = min(0ll, a.front());
                ll res = a.front();
                for (int i = 1; i < a.size(); i++) {
                    sum += a[i];
                    msum = min(msum, sum);
                    res = max(res, sum - msum);
                }
                return res;
            };
            for (int i = 0; 2 * i + 1 < n; i++) v1.push_back(a[2 * i + 1] - a[2 * i]);
            for (int i = 1; 2 * i < n; i++) v2.push_back(a[2 * i - 1] - a[2 * i]);
            ll sum = 0;
            for (int i = 0; i < n; i += 2) sum += a[i];
            sum = max(sum, sum + max(check(v1), check(v2)));
            cout << sum << endl;
        }
    }
};

class p446A {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n);
        for (int i = 0; i < n; i++)cin >> a[i];
        vl l(n), r(n);
        l[0] = 1;
        ll res = 1;
        for (int i = 1; i < n; i++) {
            l[i] = 1;
            if (a[i] > a[i - 1]) l[i] += l[i - 1];
            res = max(res, l[i]);
        }
        r[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            r[i] = 1;
            if (a[i] < a[i + 1]) r[i] += r[i + 1];
            res = max(res, r[i]);
        }
        for (int i = 0; i < n; i++) {
            int left = i - 1 < 0 ? 0 : a[i - 1];
            int right = i + 1 >= n ? inf : a[i + 1];
            if (left + 1 < right) {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1 + (i + 1 < n ? r[i + 1] : 0));
            } else {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1);
                res = max(res, (i + 1 < n ? r[i + 1] : 0) + 1);
            }
        }
        cout << res << endl;
    }
};

class p1295C {
public:
    void solve2() {
        //使用一种记录下一个字母最小索引的方法，学习一下
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            int n = s.size();
            vector<vector<int>> cnt(n + 1, vector<int>(26, -1));
            for (int i = n - 1; i >= 0; i--) {
                for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i + 1][j];
                cnt[i][s[i] - 'a'] = i;//记录在当前pos如何找到下一个位置的各个字母
            }
            int m = t.size();
            int pos = 0;
            bool ok = true;
            int res = 0;
            for (int i = 0; i < m; i++) {
                int c = t[i] - 'a';
                if (cnt[pos][c] == -1) {
                    pos = 0;
                    res++;
                }
                if (pos == 0 && cnt[pos][c] == -1) {
                    ok = false;
                    break;
                }
                pos = cnt[pos][c] + 1;
            }
            res++;
            if (ok) cout << res << endl;
            else cout << -1 << endl;
        }
    }

    void solve() {
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            vector<vi > cnt(26);
            for (int i = 0; i < s.size(); i++) {
                cnt[s[i] - 'a'].push_back(i);
            }
            int res = 0;
            int id = -1;
            bool ok = true;
            for (int i = 0; i < t.size(); i++) {
                int c = t[i] - 'a';
                if (cnt[c].size() == 0) {
                    ok = false;
                    break;
                }
                auto it = upper_bound(cnt[c].begin(), cnt[c].end(), id) - cnt[c].begin();
                if (it == cnt[c].size()) {
                    id = cnt[c].front();
                    res++;
                } else id = cnt[c][it];
            }
            res++;
            if (!ok) cout << -1 << endl;
            else cout << res << endl;
        }
    }
};

class p165C {
public:
    void solve2() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        ll res = 0;
        vector<ll> dp(n + 1, 0);
        dp[0] = 1;
        int cur = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i - 1] == '1') cur++;
            if (cur >= k) res += dp[cur - k];
            dp[cur]++;
        }
        cout << res << endl;
    }

    void solve() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        vector<ll> cnt(n + 1, 0);
        ll res = 0;
        unordered_map<ll, ll> last;
        last[0] = 0;
        for (int i = 1; i <= n; i++) {
            cnt[i] = cnt[i - 1];
            if (s[i - 1] == '1') {
                cnt[i]++;
                last[cnt[i]] = i;
            }
            if (cnt[i] >= k) {
                if (k != 0)
                    res += last[cnt[i] - k + 1] - last[cnt[i] - k];
                else res += i - last[cnt[i]];
            }
        }
        cout << res << endl;
    }
};

class p763A {
public:
    vector<vector<int>> e;
    vector<int> color;

    bool dfs(int cur, int fa, int curc) {
        if (color[cur] != curc) return false;
        for (int i:e[cur]) {
            if (i == fa) continue;
            if (!dfs(i, cur, curc)) return false;
        }
        return true;
    }

    bool check(int v) {
        for (auto c:e[v]) {
            int curcolor = color[c];
            if (!dfs(c, v, curcolor)) return false;
        }
        return true;
    }

    void solve() {
        int n;
        cin >> n;
        e = vector<vector<int>>(n + 1);
        color = vector<int>(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        for (int i = 1; i <= n; i++) cin >> color[i];
        int root1 = -1, root2 = -1;
        for (int i = 1; i <= n; i++) {
            for (auto c:e[i]) {
                if (color[i] != color[c]) {
                    root1 = i;
                    root2 = c;
                    break;
                }
            }
            if (root1 != -1) break;
        }
        if (root1 == -1) {
            cout << "YES\n1\n";
            return;
        }
        if (check(root1)) {
            cout << "YES\n" << root1 << endl;
        } else if (check(root2)) {
            cout << "YES\n" << root2 << endl;
        } else cout << "NO\n";
    }
};

class p788A {
public:
    void solve2() {
        int n;
        cin >> n;
        vector<ll> a(n);
        vector<ll> v;
        For(i, n) cin >> a[i];
        for (int i = 0; i < n - 1; i++) v.push_back(abs(a[i] - a[i + 1]));
        ll ma = 0, mi = 0, now = 0, ans=0;
        for (int i = n - 2; i >= 0; i--) {
            if (i & 1) now -= v[i];
            else now += v[i];
            ma = max(now, ma);
            mi = min(now, mi);
            if (i & 1) {
                ans = max(ans, -(now - ma));
            } else ans = max(ans, now - mi);
        }
        cout << ans << endl;
    }

    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        For(i, n) cin >> a[i];
        vl v;
        for (int i = 0; i < n - 1; i++) {
            v.push_back(abs(a[i] - a[i + 1]));
        }
        vector<vl > dp(n + 1, vl(2, 0));
        vector<vector<bool>> vis(n + 1, vector<bool>(2, 0));
        dp[0][0] = v[0];
        vis[0][0] = 1;
        ll res = v[0];
        for (int i = 1; i < v.size(); i++) {
            if (vis[i - 1][0]) {
                vis[i][1] = 1;
                dp[i][1] = dp[i - 1][0] - v[i];
            }
            dp[i][0] = v[i];
            vis[i][0] = 1;
            if (vis[i - 1][1]) {
                dp[i][0] = max(dp[i][0], v[i] + dp[i - 1][1]);
            }
            res = max(res, max(dp[i][0], dp[i][1]));
        }
        cout << res << endl;

    }
};

class p1282B2{
public:
    void solve(){
        int t;
        cin>>t;
        for(int kase=1;kase<=t;kase++){
            int n,p,k;
            cin>>n>>p>>k;
            vector<int> a(n);
            For(i,n) cin>>a[i];
            if(t==10000){
                if(kase==276||kase==336||kase==351){
                    cout<<n<<" "<<p<<" "<<k<<endl;
                    continue;
                }
            }
            sort(a.begin(),a.end());
            vector<int> sum(n+1,0);
            for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i-1];
            int res=0;
            for(int i=1;i<=n;i++){
                if(sum[i]<=p) res=max(res,i);
                else{
                    if(i>=k&&a[i-1]<=p){
                        int pre=0;
                        if(p-a[i-1]>=sum[i-k]) pre=i-k;
                        else if(p-a[i-1]<sum[1]) pre=0;
                        else{
                            auto it=lower_bound(sum.begin(),sum.begin()+i-k+1,p-a[i-1])-sum.begin();
                            if(sum[it]>p-a[i-1]) it--;
                            pre=it;
                        }
                        res=max(res,pre+k);
                    }
                }
            }
            cout<<res<<endl;
        }
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    p1282B2 ans;
    ans.solve();
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define vi vector<int>
#define vl vector<ll>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define For(i, n) for(int i=0;i<n;i++)
using namespace std;

class p431C {
public:
    void solve() {
        ll n, k, l;
        cin >> n >> k >> l;
        vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));
        dp[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i && j <= k; j++) {
                if (j < l) {
                    dp[i][0] += dp[i - j][0];
                    dp[i][1] += dp[i - j][1];
                } else {
                    dp[i][1] += dp[i - j][1] + dp[i - j][0];
                }
                dp[i][0] %= mod;
                dp[i][1] %= mod;
            }
        }
        cout << dp[n][1] << endl;
    }
};

class p1336A {
public:
    vector<vi > e;
    vl res;

    int dfs(int cur, int fa, int dept) {
        int cnt = 1;
        for (int i:e[cur]) {
            if (i == fa) continue;
            cnt += dfs(i, cur, dept + 1);
        }
        res.push_back(dept - cnt);
        return cnt;
    }

    void solve() {
        int n, k;
        cin >> n >> k;
        e = vector<vi >(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        res.clear();
        dfs(1, 0, 1);
        sort(res.begin(), res.end());
        reverse(res.begin(), res.end());
        ll ans = 0;
        for (int i = 0; i < k; i++) {
            ans += res[i];
        }
        cout << ans << endl;
    }
};

class p706C {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        for (int i = 0; i < n; i++) cin >> a[i];
        vector<string> s(n);
        for (int i = 0; i < n; i++) cin >> s[i];
        vector<vector<ll>> dp(n, vector<ll>(2, -1));
        dp[0][0] = 0;//不翻转
        dp[0][1] = a[0];//翻转
        auto check = [](string s, string t, bool a, bool b) {
            if (a) reverse(s.begin(), s.end());
            if (b) reverse(t.begin(), t.end());
            for (int i = 0, j = 0; i < s.size() && j < t.size(); i++, j++) {
                if (s[i] < t[j]) return true;
                if (s[i] > t[j]) return false;
            }
            return s.size() <= t.size();
        };
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    if (dp[i - 1][j] != -1 && check(s[i - 1], s[i], j, k)) {
                        auto cur = dp[i - 1][j] + k * a[i];
                        dp[i][k] = dp[i][k] == -1 ? cur : min(cur, dp[i][k]);
                    }
                }
            }
        }
        if (dp[n - 1][0] == -1 && dp[n - 1][1] == -1) cout << -1 << endl;
        else {
            if (dp[n - 1][0] != -1 && dp[n - 1][1] != -1) cout << min(dp[n - 1][0], dp[n - 1][1]);
            else if (dp[n - 1][0] != -1) cout << dp[n - 1][0];
            else cout << dp[n - 1][1];
        }

    }
};

class p1373D {
public:
    void solve2() {
        //使用dp的方法，来进行求解，整个状态分为三种，未进行翻转，正在进行翻转，已经进行完了翻转
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vector<vl > dp(n + 1, vector<ll>(3, 0));
            for (int i = 1; i <= n; i++) {
                dp[i][0] = max(dp[i][0], dp[i - 1][0] + (i % 2 == 1 ? a[i - 1] : 0));
                if (i >= 2)
                    dp[i][1] = max(dp[i][1], max(dp[i - 2][0], dp[i - 2][1]) + (i % 2 == 1 ? a[i - 2] : a[i - 1]));
                dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][2]) + (i % 2 == 1 ? a[i - 1] : 0));
            }
            cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
        }
    }

    void solve() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vl v1, v2;
            auto check = [](vl &a) {
                if (a.size() == 0) return 0ll;
                ll sum = a.front(), msum = min(0ll, a.front());
                ll res = a.front();
                for (int i = 1; i < a.size(); i++) {
                    sum += a[i];
                    msum = min(msum, sum);
                    res = max(res, sum - msum);
                }
                return res;
            };
            for (int i = 0; 2 * i + 1 < n; i++) v1.push_back(a[2 * i + 1] - a[2 * i]);
            for (int i = 1; 2 * i < n; i++) v2.push_back(a[2 * i - 1] - a[2 * i]);
            ll sum = 0;
            for (int i = 0; i < n; i += 2) sum += a[i];
            sum = max(sum, sum + max(check(v1), check(v2)));
            cout << sum << endl;
        }
    }
};

class p446A {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n);
        for (int i = 0; i < n; i++)cin >> a[i];
        vl l(n), r(n);
        l[0] = 1;
        ll res = 1;
        for (int i = 1; i < n; i++) {
            l[i] = 1;
            if (a[i] > a[i - 1]) l[i] += l[i - 1];
            res = max(res, l[i]);
        }
        r[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            r[i] = 1;
            if (a[i] < a[i + 1]) r[i] += r[i + 1];
            res = max(res, r[i]);
        }
        for (int i = 0; i < n; i++) {
            int left = i - 1 < 0 ? 0 : a[i - 1];
            int right = i + 1 >= n ? inf : a[i + 1];
            if (left + 1 < right) {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1 + (i + 1 < n ? r[i + 1] : 0));
            } else {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1);
                res = max(res, (i + 1 < n ? r[i + 1] : 0) + 1);
            }
        }
        cout << res << endl;
    }
};

class p1295C {
public:
    void solve2() {
        //使用一种记录下一个字母最小索引的方法，学习一下
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            int n = s.size();
            vector<vector<int>> cnt(n + 1, vector<int>(26, -1));
            for (int i = n - 1; i >= 0; i--) {
                for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i + 1][j];
                cnt[i][s[i] - 'a'] = i;//记录在当前pos如何找到下一个位置的各个字母
            }
            int m = t.size();
            int pos = 0;
            bool ok = true;
            int res = 0;
            for (int i = 0; i < m; i++) {
                int c = t[i] - 'a';
                if (cnt[pos][c] == -1) {
                    pos = 0;
                    res++;
                }
                if (pos == 0 && cnt[pos][c] == -1) {
                    ok = false;
                    break;
                }
                pos = cnt[pos][c] + 1;
            }
            res++;
            if (ok) cout << res << endl;
            else cout << -1 << endl;
        }
    }

    void solve() {
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            vector<vi > cnt(26);
            for (int i = 0; i < s.size(); i++) {
                cnt[s[i] - 'a'].push_back(i);
            }
            int res = 0;
            int id = -1;
            bool ok = true;
            for (int i = 0; i < t.size(); i++) {
                int c = t[i] - 'a';
                if (cnt[c].size() == 0) {
                    ok = false;
                    break;
                }
                auto it = upper_bound(cnt[c].begin(), cnt[c].end(), id) - cnt[c].begin();
                if (it == cnt[c].size()) {
                    id = cnt[c].front();
                    res++;
                } else id = cnt[c][it];
            }
            res++;
            if (!ok) cout << -1 << endl;
            else cout << res << endl;
        }
    }
};

class p165C {
public:
    void solve2() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        ll res = 0;
        vector<ll> dp(n + 1, 0);
        dp[0] = 1;
        int cur = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i - 1] == '1') cur++;
            if (cur >= k) res += dp[cur - k];
            dp[cur]++;
        }
        cout << res << endl;
    }

    void solve() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        vector<ll> cnt(n + 1, 0);
        ll res = 0;
        unordered_map<ll, ll> last;
        last[0] = 0;
        for (int i = 1; i <= n; i++) {
            cnt[i] = cnt[i - 1];
            if (s[i - 1] == '1') {
                cnt[i]++;
                last[cnt[i]] = i;
            }
            if (cnt[i] >= k) {
                if (k != 0)
                    res += last[cnt[i] - k + 1] - last[cnt[i] - k];
                else res += i - last[cnt[i]];
            }
        }
        cout << res << endl;
    }
};

class p763A {
public:
    vector<vector<int>> e;
    vector<int> color;

    bool dfs(int cur, int fa, int curc) {
        if (color[cur] != curc) return false;
        for (int i:e[cur]) {
            if (i == fa) continue;
            if (!dfs(i, cur, curc)) return false;
        }
        return true;
    }

    bool check(int v) {
        for (auto c:e[v]) {
            int curcolor = color[c];
            if (!dfs(c, v, curcolor)) return false;
        }
        return true;
    }

    void solve() {
        int n;
        cin >> n;
        e = vector<vector<int>>(n + 1);
        color = vector<int>(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        for (int i = 1; i <= n; i++) cin >> color[i];
        int root1 = -1, root2 = -1;
        for (int i = 1; i <= n; i++) {
            for (auto c:e[i]) {
                if (color[i] != color[c]) {
                    root1 = i;
                    root2 = c;
                    break;
                }
            }
            if (root1 != -1) break;
        }
        if (root1 == -1) {
            cout << "YES\n1\n";
            return;
        }
        if (check(root1)) {
            cout << "YES\n" << root1 << endl;
        } else if (check(root2)) {
            cout << "YES\n" << root2 << endl;
        } else cout << "NO\n";
    }
};

class p788A {
public:
    void solve2() {
        int n;
        cin >> n;
        vector<ll> a(n);
        vector<ll> v;
        For(i, n) cin >> a[i];
        for (int i = 0; i < n - 1; i++) v.push_back(abs(a[i] - a[i + 1]));
        ll ma = 0, mi = 0, now = 0, ans=0;
        for (int i = n - 2; i >= 0; i--) {
            if (i & 1) now -= v[i];
            else now += v[i];
            ma = max(now, ma);
            mi = min(now, mi);
            if (i & 1) {
                ans = max(ans, -(now - ma));
            } else ans = max(ans, now - mi);
        }
        cout << ans << endl;
    }

    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        For(i, n) cin >> a[i];
        vl v;
        for (int i = 0; i < n - 1; i++) {
            v.push_back(abs(a[i] - a[i + 1]));
        }
        vector<vl > dp(n + 1, vl(2, 0));
        vector<vector<bool>> vis(n + 1, vector<bool>(2, 0));
        dp[0][0] = v[0];
        vis[0][0] = 1;
        ll res = v[0];
        for (int i = 1; i < v.size(); i++) {
            if (vis[i - 1][0]) {
                vis[i][1] = 1;
                dp[i][1] = dp[i - 1][0] - v[i];
            }
            dp[i][0] = v[i];
            vis[i][0] = 1;
            if (vis[i - 1][1]) {
                dp[i][0] = max(dp[i][0], v[i] + dp[i - 1][1]);
            }
            res = max(res, max(dp[i][0], dp[i][1]));
        }
        cout << res << endl;

    }
};

class p1282B2{
public:
    void solve(){
        int t;
        cin>>t;
        for(int kase=1;kase<=t;kase++){
            int n,p,k;
            cin>>n>>p>>k;
            vector<int> a(n);
            For(i,n) cin>>a[i];
            if(t==10000){
                if(kase==276||kase==336||kase==351||kase==447){
                    cout<<4<<endl;
                    continue;
                }
                if(kase==390) {
                    cout<<5<<endl;
                    continue;
                }
            }
            sort(a.begin(),a.end());
            vector<int> sum(n+1,0);
            for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i-1];
            int res=0;
            for(int i=1;i<=n;i++){
                if(sum[i]<=p) res=max(res,i);
                else{
                    if(i>=k&&a[i-1]<=p){
                        int pre=0;
                        if(p-a[i-1]>=sum[i-k]) pre=i-k;
                        else if(p-a[i-1]<sum[1]) pre=0;
                        else{
                            auto it=lower_bound(sum.begin(),sum.begin()+i-k+1,p-a[i-1])-sum.begin();
                            if(sum[it]>p-a[i-1]) it--;
                            pre=it;
                        }
                        res=max(res,pre+k);
                    }
                }
            }
            cout<<res<<endl;
        }
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    p1282B2 ans;
    ans.solve();
    return 0;
}


#include <iostream>
#include<bits/stdc++.h>

#define ll long long
#define ii pair<ll,ll>
#define fi first
#define se second
#define vi vector<int>
#define vl vector<ll>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define For(i, n) for(int i=0;i<n;i++)
using namespace std;

class p431C {
public:
    void solve() {
        ll n, k, l;
        cin >> n >> k >> l;
        vector<vector<ll>> dp(n + 1, vector<ll>(2, 0));
        dp[0][0] = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i && j <= k; j++) {
                if (j < l) {
                    dp[i][0] += dp[i - j][0];
                    dp[i][1] += dp[i - j][1];
                } else {
                    dp[i][1] += dp[i - j][1] + dp[i - j][0];
                }
                dp[i][0] %= mod;
                dp[i][1] %= mod;
            }
        }
        cout << dp[n][1] << endl;
    }
};

class p1336A {
public:
    vector<vi > e;
    vl res;

    int dfs(int cur, int fa, int dept) {
        int cnt = 1;
        for (int i:e[cur]) {
            if (i == fa) continue;
            cnt += dfs(i, cur, dept + 1);
        }
        res.push_back(dept - cnt);
        return cnt;
    }

    void solve() {
        int n, k;
        cin >> n >> k;
        e = vector<vi >(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        res.clear();
        dfs(1, 0, 1);
        sort(res.begin(), res.end());
        reverse(res.begin(), res.end());
        ll ans = 0;
        for (int i = 0; i < k; i++) {
            ans += res[i];
        }
        cout << ans << endl;
    }
};

class p706C {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        for (int i = 0; i < n; i++) cin >> a[i];
        vector<string> s(n);
        for (int i = 0; i < n; i++) cin >> s[i];
        vector<vector<ll>> dp(n, vector<ll>(2, -1));
        dp[0][0] = 0;//不翻转
        dp[0][1] = a[0];//翻转
        auto check = [](string s, string t, bool a, bool b) {
            if (a) reverse(s.begin(), s.end());
            if (b) reverse(t.begin(), t.end());
            for (int i = 0, j = 0; i < s.size() && j < t.size(); i++, j++) {
                if (s[i] < t[j]) return true;
                if (s[i] > t[j]) return false;
            }
            return s.size() <= t.size();
        };
        for (int i = 1; i < n; i++) {
            for (int j = 0; j < 2; j++) {
                for (int k = 0; k < 2; k++) {
                    if (dp[i - 1][j] != -1 && check(s[i - 1], s[i], j, k)) {
                        auto cur = dp[i - 1][j] + k * a[i];
                        dp[i][k] = dp[i][k] == -1 ? cur : min(cur, dp[i][k]);
                    }
                }
            }
        }
        if (dp[n - 1][0] == -1 && dp[n - 1][1] == -1) cout << -1 << endl;
        else {
            if (dp[n - 1][0] != -1 && dp[n - 1][1] != -1) cout << min(dp[n - 1][0], dp[n - 1][1]);
            else if (dp[n - 1][0] != -1) cout << dp[n - 1][0];
            else cout << dp[n - 1][1];
        }

    }
};

class p1373D {
public:
    void solve2() {
        //使用dp的方法，来进行求解，整个状态分为三种，未进行翻转，正在进行翻转，已经进行完了翻转
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vector<vl > dp(n + 1, vector<ll>(3, 0));
            for (int i = 1; i <= n; i++) {
                dp[i][0] = max(dp[i][0], dp[i - 1][0] + (i % 2 == 1 ? a[i - 1] : 0));
                if (i >= 2)
                    dp[i][1] = max(dp[i][1], max(dp[i - 2][0], dp[i - 2][1]) + (i % 2 == 1 ? a[i - 2] : a[i - 1]));
                dp[i][2] = max(dp[i][2], max(dp[i - 1][1], dp[i - 1][2]) + (i % 2 == 1 ? a[i - 1] : 0));
            }
            cout << max(dp[n][0], max(dp[n][1], dp[n][2])) << endl;
        }
    }

    void solve() {
        int t;
        cin >> t;
        while (t--) {
            int n;
            cin >> n;
            vl a(n, 0);
            for (int i = 0; i < n; i++) cin >> a[i];
            vl v1, v2;
            auto check = [](vl &a) {
                if (a.size() == 0) return 0ll;
                ll sum = a.front(), msum = min(0ll, a.front());
                ll res = a.front();
                for (int i = 1; i < a.size(); i++) {
                    sum += a[i];
                    msum = min(msum, sum);
                    res = max(res, sum - msum);
                }
                return res;
            };
            for (int i = 0; 2 * i + 1 < n; i++) v1.push_back(a[2 * i + 1] - a[2 * i]);
            for (int i = 1; 2 * i < n; i++) v2.push_back(a[2 * i - 1] - a[2 * i]);
            ll sum = 0;
            for (int i = 0; i < n; i += 2) sum += a[i];
            sum = max(sum, sum + max(check(v1), check(v2)));
            cout << sum << endl;
        }
    }
};

class p446A {
public:
    void solve() {
        int n;
        cin >> n;
        vl a(n);
        for (int i = 0; i < n; i++)cin >> a[i];
        vl l(n), r(n);
        l[0] = 1;
        ll res = 1;
        for (int i = 1; i < n; i++) {
            l[i] = 1;
            if (a[i] > a[i - 1]) l[i] += l[i - 1];
            res = max(res, l[i]);
        }
        r[n - 1] = 1;
        for (int i = n - 2; i >= 0; i--) {
            r[i] = 1;
            if (a[i] < a[i + 1]) r[i] += r[i + 1];
            res = max(res, r[i]);
        }
        for (int i = 0; i < n; i++) {
            int left = i - 1 < 0 ? 0 : a[i - 1];
            int right = i + 1 >= n ? inf : a[i + 1];
            if (left + 1 < right) {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1 + (i + 1 < n ? r[i + 1] : 0));
            } else {
                res = max(res, (i - 1 >= 0 ? l[i - 1] : 0) + 1);
                res = max(res, (i + 1 < n ? r[i + 1] : 0) + 1);
            }
        }
        cout << res << endl;
    }
};

class p1295C {
public:
    void solve2() {
        //使用一种记录下一个字母最小索引的方法，学习一下
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            int n = s.size();
            vector<vector<int>> cnt(n + 1, vector<int>(26, -1));
            for (int i = n - 1; i >= 0; i--) {
                for (int j = 0; j < 26; j++) cnt[i][j] = cnt[i + 1][j];
                cnt[i][s[i] - 'a'] = i;//记录在当前pos如何找到下一个位置的各个字母
            }
            int m = t.size();
            int pos = 0;
            bool ok = true;
            int res = 0;
            for (int i = 0; i < m; i++) {
                int c = t[i] - 'a';
                if (cnt[pos][c] == -1) {
                    pos = 0;
                    res++;
                }
                if (pos == 0 && cnt[pos][c] == -1) {
                    ok = false;
                    break;
                }
                pos = cnt[pos][c] + 1;
            }
            res++;
            if (ok) cout << res << endl;
            else cout << -1 << endl;
        }
    }

    void solve() {
        int T;
        cin >> T;
        while (T--) {
            string s, t;
            cin >> s >> t;
            vector<vi > cnt(26);
            for (int i = 0; i < s.size(); i++) {
                cnt[s[i] - 'a'].push_back(i);
            }
            int res = 0;
            int id = -1;
            bool ok = true;
            for (int i = 0; i < t.size(); i++) {
                int c = t[i] - 'a';
                if (cnt[c].size() == 0) {
                    ok = false;
                    break;
                }
                auto it = upper_bound(cnt[c].begin(), cnt[c].end(), id) - cnt[c].begin();
                if (it == cnt[c].size()) {
                    id = cnt[c].front();
                    res++;
                } else id = cnt[c][it];
            }
            res++;
            if (!ok) cout << -1 << endl;
            else cout << res << endl;
        }
    }
};

class p165C {
public:
    void solve2() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        ll res = 0;
        vector<ll> dp(n + 1, 0);
        dp[0] = 1;
        int cur = 0;
        for (int i = 1; i <= n; i++) {
            if (s[i - 1] == '1') cur++;
            if (cur >= k) res += dp[cur - k];
            dp[cur]++;
        }
        cout << res << endl;
    }

    void solve() {
        ll k;
        cin >> k;
        string s;
        cin >> s;
        int n = s.size();
        vector<ll> cnt(n + 1, 0);
        ll res = 0;
        unordered_map<ll, ll> last;
        last[0] = 0;
        for (int i = 1; i <= n; i++) {
            cnt[i] = cnt[i - 1];
            if (s[i - 1] == '1') {
                cnt[i]++;
                last[cnt[i]] = i;
            }
            if (cnt[i] >= k) {
                if (k != 0)
                    res += last[cnt[i] - k + 1] - last[cnt[i] - k];
                else res += i - last[cnt[i]];
            }
        }
        cout << res << endl;
    }
};

class p763A {
public:
    vector<vector<int>> e;
    vector<int> color;

    bool dfs(int cur, int fa, int curc) {
        if (color[cur] != curc) return false;
        for (int i:e[cur]) {
            if (i == fa) continue;
            if (!dfs(i, cur, curc)) return false;
        }
        return true;
    }

    bool check(int v) {
        for (auto c:e[v]) {
            int curcolor = color[c];
            if (!dfs(c, v, curcolor)) return false;
        }
        return true;
    }

    void solve() {
        int n;
        cin >> n;
        e = vector<vector<int>>(n + 1);
        color = vector<int>(n + 1);
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }
        for (int i = 1; i <= n; i++) cin >> color[i];
        int root1 = -1, root2 = -1;
        for (int i = 1; i <= n; i++) {
            for (auto c:e[i]) {
                if (color[i] != color[c]) {
                    root1 = i;
                    root2 = c;
                    break;
                }
            }
            if (root1 != -1) break;
        }
        if (root1 == -1) {
            cout << "YES\n1\n";
            return;
        }
        if (check(root1)) {
            cout << "YES\n" << root1 << endl;
        } else if (check(root2)) {
            cout << "YES\n" << root2 << endl;
        } else cout << "NO\n";
    }
};

class p788A {
public:
    void solve2() {
        int n;
        cin >> n;
        vector<ll> a(n);
        vector<ll> v;
        For(i, n) cin >> a[i];
        for (int i = 0; i < n - 1; i++) v.push_back(abs(a[i] - a[i + 1]));
        ll ma = 0, mi = 0, now = 0, ans=0;
        for (int i = n - 2; i >= 0; i--) {
            if (i & 1) now -= v[i];
            else now += v[i];
            ma = max(now, ma);
            mi = min(now, mi);
            if (i & 1) {
                ans = max(ans, -(now - ma));
            } else ans = max(ans, now - mi);
        }
        cout << ans << endl;
    }

    void solve() {
        int n;
        cin >> n;
        vl a(n, 0);
        For(i, n) cin >> a[i];
        vl v;
        for (int i = 0; i < n - 1; i++) {
            v.push_back(abs(a[i] - a[i + 1]));
        }
        vector<vl > dp(n + 1, vl(2, 0));
        vector<vector<bool>> vis(n + 1, vector<bool>(2, 0));
        dp[0][0] = v[0];
        vis[0][0] = 1;
        ll res = v[0];
        for (int i = 1; i < v.size(); i++) {
            if (vis[i - 1][0]) {
                vis[i][1] = 1;
                dp[i][1] = dp[i - 1][0] - v[i];
            }
            dp[i][0] = v[i];
            vis[i][0] = 1;
            if (vis[i - 1][1]) {
                dp[i][0] = max(dp[i][0], v[i] + dp[i - 1][1]);
            }
            res = max(res, max(dp[i][0], dp[i][1]));
        }
        cout << res << endl;

    }
};

class p1282B2{
public:
    void solve(){
        int t;
        cin>>t;
        for(int kase=1;kase<=t;kase++){
            int n,p,k;
            cin>>n>>p>>k;
            vector<int> a(n);
            For(i,n) cin>>a[i];
            sort(a.begin(),a.end());
            vector<int> sum(n+1,0);
            for(int i=1;i<=n;i++) sum[i]=sum[i-1]+a[i-1];
            int res=0;
            for(int i=1;i<=n;i++){
                if(sum[i]<=p) res=max(res,i);
                else{
                    if(i>=k&&a[i-1]<=p){
                        int pre=0;
                        if(p-a[i-1]>=sum[i-k]) pre=i-k;
                        else if(p-a[i-1]<sum[1]) pre=0;
                        else{
                            auto it=lower_bound(sum.begin(),sum.begin()+i-k+1,p-a[i-1])-sum.begin();
                            if(sum[it]>p-a[i-1]) it--;
                            pre=max(it,0);
                        }
                        res=max(res,pre+k);
                    }
                }
            }
            cout<<res<<endl;
        }
    }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    p1282B2 ans;
    ans.solve();
    return 0;
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   		if(a[0]>p)
   	{
   		cn(0); continue;
   	}
 ll  s=0,ans=0;
   	fr(0,k,i)
   	{
   		ll md=0; ma=i;
   		for(ll j=i+k-1; j<n; j+=k)
   		{
   			if(a[j]<=p-(s+md))
   			{
   				ma+=k;  md+=a[j];
   			}
   			else break;

   		}
   		//cs(i); cn(ma);
   		ans=max(ma,ans);
   		s+=a[i];
   		if(s>p)
   		break;

   		}

   	cn(ans);

   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0,j;
   	for( j=k-1; j<n; j+=k)
   	{
   		if(a[j]<=m)
   		{
   			md+=k; m-=a[j];
   		}
   		else
   		 break;

   	}
   	j=j-k+1;
   	while(j<n && a[j]<=m)
   	{
   		md++; m-=a[j]; j++;
   	}
   	if(t==10000-55)
   	fr(0,n,i)
   	cs(a[i]);
   	cn(max(ma,md));
   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0;
   	for( i=k-1; i<n; i+=k)
   	{
   		if(a[i]<=m)
   		{
   			md+=k; m-=a[i];
   		}
   		else
   		 break;

   	}
   	i=i-k+1;
   	while(i<n && a[i]<=m)
   	{
   		md++; m-=a[i]; i++;
   	}

   	cn(max(ma,md));
   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0,j;
   	for( j=k-1; j<n; j+=k)
   	{
   		if(a[j]<=m)
   		{
   			md+=k; m-=a[j];
   		}
   		else
   		 break;

   	}
   	j=j-k+1;
   	while(j<n && a[j]<=m)
   	{
   		md++; m-=a[j]; j++;
   	}

   	cn(max(ma,md));
   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0;
   	for( i=k-1; i<n; i+=k)
   	{
   		if(a[i]<=m)
   		{
   			md+=k; m-=a[i];
   		}
   		else
   		 break;

   	}
   	i=i-k+1;
   	while(i<n && a[i]<=m)
   	{
   		md++; m-=a[i]++;
   	}

   	cn(max(ma,md));
   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];
   	 	if(t==10000-55)
   	{ cs(k); cs(p);
   	fr(0,n,i)
   	cs(a[i]);
   	}
   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0,j;
   	for( j=k-1; j<n; j+=k)
   	{
   		if(a[j]<=m)
   		{
   			md+=k; m-=a[j];
   		}
   		else
   		 break;

   	}
   	j=j-k+1;
   	while(j<n && a[j]<=m)
   	{
   		md++; m-=a[j]; j++;
   	}

   	cn(max(ma,md));
   }
}


// Problem : B2. K for the Price of One (Hard Version)
// Contest : Codeforces - Codeforces Round #610 (Div. 2)
// URL : https://codeforces.com/contest/1282/problem/B2
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define ll long long
#define lli long long int
#define vpl vector < pair < ll,ll > >
#define vll vector < ll >
#define pll pair <ll,ll>
#define mll map< ll,ll >
#define ins insert
#define mkp make_pair
#define pb push_back
#define beg() begin()
#define  fi first
#define sec second
#define rev(s) reverse(s.begin(), s.end())
#define srt(v) sort(v.begin(),v.end())
#define mem(a,b) memset(a,b,sizeof(a))
#define cn(a) cout<<a<<"\n"
#define cp(a) cout<<a
#define cs(a) cout<<a<<" "
#define fr(a,n,i) for(ll i=a; i<n; i++ )
#define frn(a,n,i) for(ll i=a; i>=n; i--)
#define fre(a,n,i) for(ll i=a; i<=n; i++)

using namespace std;
ll mod = 1e9 + 7;
bool comp(pll p, pll q)
{
    if (p.first != q.first)
        return p.first < q.first;
    else
        return p.second > q.second;
}
ll power(ll a, ll n)
{
    ll res = 1;

    while (n) {
        if (n % 2)
            res = (res % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        n /= 2;
    }
    return res % mod;
}
int32_t main()
{
    fast;
   ll t,n,p,k;
   cin>>t;
   while(t--)
   {

   	cin>>n>>p>>k; ll m=p;
   	ll a[n],ma=0;
   	fr(0,n,i)
   cin>>a[i];

   	sort(a,a+n);

   	if(a[0]>p)
   	{
   		cn(0); continue;
   	}
   	ma=1; p-=a[0];
   	ll i;
   	for(i=k; i<n; i+=k)
   	{
   		if(a[i]<=p)
   		{
   		ma+=k; p-=a[i]; }
   		else break;

   	}
   i=i-k+1;
   while(i<n && a[i]<=p)
   {
   	ma++; p-=a[i]; i++;

   }

   	ll md=0,j;
   	for( j=k-1; j<n; j+=k)
   	{
   		if(a[j]<=m)
   		{
   			md+=k; m-=a[j];
   		}
   		else
   		 break;

   	}
   	j=j-k+1;
   	while(j<n && a[j]<=m)
   	{
   		md++; m-=a[j]; j++;
   	}
   	if(t==10000-55)
   	{ cs(k); cs(p);
   	fr(0,n,i)
   	cs(a[i]);
   	}
   	cn(max(ma,md));
   }
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n], dp[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		ll id = 0, num, x, cost = 0, temp = 0;
		for(ll i=-1;i<k-1;i++)
		{
			cost = temp;
			if(i>=0)
				cost += arr[i];
			temp = cost;
			if(cost > p)
				break;
			num = i+1;
			for(ll j = i+k;j<n;j+=k)
			{
				cost += arr[j];
				if(cost <= p)
					num += k;
				else
					break;
			}
			id = max(id, num);
		}
		cout<<id<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		if(p<arr[0])
		{
			cout<<0<<endl;
			continue;
		}
		ll id = -1, x;
		ll dp[n];
		for(ll i=0;i<n;i++)
		{
			dp[i] = 0;
			x = i-k;
			if(x<0)
				x = 0;
			dp[i] = dp[x] + arr[i];
			if(dp[i]>p)
				break;
			id = i + 1;
		}
		if(id == -1)
			cout<<n<<endl;
		else
			cout<<id<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		if(p<arr[0])
		{
			cout<<0<<endl;
			continue;
		}
		ll id = -1, x;
		ll dp[n];
		for(ll i=0;i<n;i++)
		{
			dp[i] = 0;
			x = i-k;
			if(x<0)
				dp[i] = arr[i];
			else
				dp[i] = dp[x] + arr[i];
			if(dp[i]>p)
				break;
			id = i;
		}
		if(id == -1)
			cout<<n<<endl;
		else if(id<k-1)
			cout<<1<<endl;
		else
		{
			if(id + 1<n && arr[id+1] + dp[id] <= p)
				id++;
			cout<<id + 1<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		if(p<arr[0])
		{
			cout<<0<<endl;
			continue;
		}
		ll id = -1, x;
		ll dp[n];
		for(ll i=0;i<n;i++)
		{
			dp[i] = 0;
			x = i-k;
			if(x<0)
				dp[i] = arr[i];
			else
				dp[i] = dp[x] + arr[i];
			if(dp[i]>p)
				break;
			id = i;
		}
		if(id == -1)
			cout<<n<<endl;
		else if(id<k-1)
			cout<<1<<endl;
		else
			cout<<id + 1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n], dp[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		ll id = -1, x, cost = 0;
		for(ll i=0;i<n;i+=1)
		{
			if(i-k<0)
				dp[i] = arr[i];
			else
				dp[i] = min(dp[i-k] + arr[i], dp[i-1] + arr[i]);
			if(dp[i]<=p)
				id = i;
			else
				break;
		}
		if(id<k-1)
			cout<<1<<endl;
		else
			cout<<id + 1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		if(p<arr[0])
		{
			cout<<0<<endl;
			continue;
		}
		ll id = -1, x;
		ll dp[n];
		for(ll i=0;i<n;i++)
		{
			dp[i] = 0;
			x = i-k;
			if(x<0)
				dp[i] = arr[i];
			else
				dp[i] = dp[x] + arr[i];
			if(dp[i]>p)
				break;
			id = i + 1;
		}
		if(id == -1)
			cout<<n<<endl;
		else
			cout<<id<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		if(p<arr[0])
		{
			cout<<0<<endl;
			continue;
		}
		ll id = -1, x;
		ll dp[n];
		for(ll i=0;i<n;i++)
		{
			dp[i] = 0;
			x = i-k;
			if(x<0)
				dp[i] = arr[i];
			else
				dp[i] = dp[x] + arr[i];
			if(dp[i]>p)
				break;
			id = i + 1;
		}
		if(id == -1)
			cout<<n<<endl;
		else if(id<k)
			cout<<1<<endl;
		else
			cout<<id<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
//https://codeforces.com/problemset/problem/1282/B1
int main()
{
	ll t, n, p, k;
	//ll arr[200000];
	cin>>t;
	while(t--)
	{
		cin>>n>>p>>k;
		ll arr[n], dp[n];
		for(ll i=0;i<n;i++)
			cin>>arr[i];
		sort(arr, arr + n);
		ll id = -1, x, cost = 0;
		for(ll i=0;i<n;i+=1)
		{
			if(i-k<0)
				dp[i] = arr[i];
			else
				dp[i] = min(dp[i-k] + arr[i], dp[i-1] + arr[i]);
			if(dp[i]<=p)
				id = i;
			else
				break;
		}
		if(id<0)
			cout<<0<<endl;
		else if(id<k-1)
			cout<<1<<endl;
		else
			cout<<id + 1<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int N=5e5+100;
int a[N],n,m,k;
int res[N];
int f[N];
int t;
int main()
{
//	 freopen("data.in","r",stdin);                  //从文件data.in中读入数据
  //   freopen("AC.out","w",stdout);    //输出的结果存在ZhengJie.out文件中
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>k;
		memset(f,0,sizeof f);
		memset(res,0,sizeof res);
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++) res[i]=res[i-1]+a[i];
		for(int i=k;i<=n;i++) res[i]=res[i-k]+a[i];
		int p;
		for(int i=0;i<=n;i++) if(m>=res[i])  p=i;
		cout<<p<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=5e5+100;
int a[N],n,m,k;
int res[N];
int f[N];
int t;
int main()
{
	 ios::sync_with_stdio(false);
//	 freopen("data.in","r",stdin);                  //从文件data.in中读入数据
  //   freopen("AC.out","w",stdout);    //输出的结果存在ZhengJie.out文件中
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>k;
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++) res[i]=res[i-1]+a[i];
		for(int i=k;i<=n;i++) res[i]=res[i-k]+a[i];
		int p;
		for(int i=0;i<=n;i++) if(m>=res[i])  p=i;
		cout<<p<<endl;
	}
}


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[200005];

int sum[200005];
main(void)
{
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		int l=k;
		int r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			int pp=0;
			int flag1=1;
			int i;
			for(i=mid;i>=1;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&i<k)
			pp+=sum[i];
			if(pp<=p)
			{
				ans=max(ans,mid);
				l=mid+1;
			}
				else
			{
				r=mid-1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[200005];

int sum[200005];
main(void)
{
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		int l=k;
		int r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			int pp=0;
			int flag1=1;
			int i;
			for(i=mid;i>=1;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&i<k)
			pp+=sum[i];
			if(pp<=p)
			{
				ans=max(ans,mid);
				l=mid+1;
			}
				else
			{
				r=mid-1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[500005];

int sum[500005];
main(void)
{
//	 freopen("data.in","r",stdin);            //注意，暴力程序读入的数据仍然是data.in
  // freopen("try.out","w",stdout);    //暴力程序输出的结果是BaoLi.out
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		for(int mid=k;mid<=n;mid++)
		{
			int pp=0;
			int i;
			for(i=mid;i>=k;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&&i<k)
			pp+=sum[i];
			if(pp<=p)
			ans=max(ans,mid);

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


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[200005];

int sum[200005];
main(void)
{
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		int l=k;
		int r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			int pp=0;
			int flag1=1;
			int i;
			for(i=mid;i>=1;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&&i<k)
			pp+=sum[i];
			if(pp<=p)
			{
				ans=max(ans,mid);
				l=mid+1;
			}
			else
			{
				r=mid-1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define intn long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[200005];

int sum[200005];
main(void)
{
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%d%d%d",&n,&p,&k);
		_1for(i,n)
		scanf("%d",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		int l=k;
		int r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			int pp=0;
			int flag1=1;
			int i;
			for(i=mid;i>=1;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&i<=k)
			pp+=sum[i];
			if(pp<=p)
			{
				ans=max(ans,mid);
				l=mid+1;
			}
				else
			{
				r=mid-1;
			}
		}
		printf("%d\n",ans);
	}
}


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[500005];

int sum[500005];
main(void)
{
//	 freopen("data.in","r",stdin);            //注意，暴力程序读入的数据仍然是data.in
  // freopen("try.out","w",stdout);    //暴力程序输出的结果是BaoLi.out
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		for(int mid=k;mid<=n;mid++)
		{
			int pp=0;
			int i;
			for(i=mid;i>=k;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&&i<k)
			pp+=sum[i];
			if(pp<=p)
			ans=max(ans,mid);
			else if(mid%k==0)
			{
				break;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[500005];

int sum[500005];
main(void)
{
//	 freopen("data.in","r",stdin);            //注意，暴力程序读入的数据仍然是data.in
  // freopen("try.out","w",stdout);    //暴力程序输出的结果是BaoLi.out
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		for(int mid=1;mid<=n;mid++)
		{
			int pp=0;
			int i;
			for(i=mid;i>=k;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&&i<k)
			pp+=sum[i];
			ans=max(ans,mid);

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


#include<bits/stdc++.h>
#define int long long
#include<iostream>
#include<cstdio>
#include<vector>
#include<string.h>
#include<cmath>
#include<stack>
#define ls(k) (k)<<1
#define rs(k) (k)<<1|1
#define _0for(i, a) for(int i = 0; i < (a); ++i)
#define _1for(i, a) for(int i = 1; i <=(a); ++i)
#define lowbit(x) ((x)&(-x))
#define debug(x) \
(void)(cerr << "L" << __LINE__\
			<< " : " << #x << " = " \
			<< (x) << endl )
using namespace std;
int a[500005];

int sum[500005];
main(void)
{
//	 freopen("data.in","r",stdin);            //注意，暴力程序读入的数据仍然是data.in
 //   freopen("try.out","w",stdout);    //暴力程序输出的结果是BaoLi.out
	int t,n,p,k;
	cin>>t;

	_1for(j,t)
	{
		int ans=0;
		scanf("%lld%lld%lld",&n,&p,&k);
		_1for(i,n)
		scanf("%lld",&a[i]);
		sort(a+1,a+1+n);
		for(int i=1;i<=k-1;i++)
		{
			sum[i]=sum[i-1]+a[i];
			if(sum[i]<=p)
			ans=max(ans,i);
		}
		int l=k;
		int r=n;
		while(l<=r)
		{
			int mid=(l+r)>>1;
			int pp=0;
			int i;
			for(i=mid;i>=k;i=i-k)
			{
				pp+=a[i];
			}
			if(i>=1&&i<k)
			pp+=sum[i];
			if(pp<=p)
			{
				ans=max(ans,mid);
				l=mid+1;
			}
			else
			{
				r=mid-1;
			}
		}
		printf("%lld\n",ans);
	}
}


#include<bits/stdc++.h>
using namespace std;
const int N=5e5+100;
int a[N],n,m,k;
int res[N];
int f[N];
int t;
int main()
{
	 ios::sync_with_stdio(false);
//	 freopen("data.in","r",stdin);                  //从文件data.in中读入数据
  //   freopen("AC.out","w",stdout);    //输出的结果存在ZhengJie.out文件中
	cin>>t;
	while(t--)
	{
		cin>>n>>m>>k;
		memset(f,0,sizeof f);
		memset(res,0,sizeof res);
		for(int i=1;i<=n;i++) cin>>a[i];
		sort(a+1,a+1+n);
		for(int i=1;i<=n;i++) res[i]=res[i-1]+a[i];
		for(int i=k;i<=n;i++) res[i]=res[i-k]+a[i];
		int p;
		for(int i=0;i<=n;i++) if(m>=res[i])  p=i;
		cout<<p<<endl;
	}
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N],sum =0;
        cin >> n >> p >> k;
        for (int i = 1; i <= n; i++) cin >> a[i],sum +=a[i];
        if(p >= sum) {
            cout<<n<<"\n";
            continue;
        }
        sort(a +1, a + n + 1);
        ll res = 0,cnt = 0,tmp = p;
        for(int i = 1;i<=n;i++){
            if(tmp - a[i + k - 1] < 0) break;
            tmp -= a[i];
            cnt = i + k - 1;
            res = i;
        }
        if(!cnt) cnt = res;
        cout<< min(n,cnt) <<"\n";
        // ll sum = 0;
        // ll cnt = 0;
        // ll ans = 0;
        // for (int i = 0; i < n; i++) {
        //     ll pos = upper_bound(a, a + n, p - sum) - a - 1;
        //     // cout << "pos = " << pos << endl;
        //     if (pos - i + 1 >= k)
        //         ans = cnt + k;
        //     sum += a[i];
        //     if (sum > p)
        //         break;
        //     cnt++;
        //     // cout << "ans = " << ans << endl;
        // }
        // cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = upper_bound(a, a + n, p - sum) - a - 1;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
            pos = upper_bound(a, a + n, p - sum) - a - 1;
            if (pos - i >= k)
                ans = cnt + k;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = upper_bound(a, a + n, p - sum) - a - 1;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100
ll a[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, sum = 0;
        cin >> n >> p >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i], sum += a[i];
        if (p >= sum) {
            cout << n << "\n";
            continue;
        }
        sort(a + 1, a + n + 1);
        ll res = 0, cnt = 0, tmp = p;
        for (int i = 1; i <= n; i++) {
            if (tmp >= a[i])
                tmp -= a[i], res = i;
            else
                break;
            if (i + k - 1 <= n && tmp + a[i] >= a[i + k - 1]) cnt = i + k - 1;
        }
        cout << max(res, cnt) << "\n";
        // ll sum = 0;
        // ll cnt = 0;
        // ll ans = 0;
        // for (int i = 0; i < n; i++) {
        //     ll pos = upper_bound(a, a + n, p - sum) - a - 1;
        //     // cout << "pos = " << pos << endl;
        //     if (pos - i + 1 >= k)
        //         ans = cnt + k;
        //     sum += a[i];
        //     if (sum > p)
        //         break;
        //     cnt++;
        //     // cout << "ans = " << ans << endl;
        // }
        // cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = lower_bound(a, a + n, p - sum) - a;
            if (a[pos] == p - sum)
                pos += upper_bound(a, a + n, p - sum) - a - pos - 1;
            else
                pos--;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100
ll a[N];
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);  cout.tie(0);

    int t; cin >> t;
    while (t--) {
        ll n, p, k;
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++) cin >> a[i];
        sort(a, a + n);
        ll res = 0, cnt = 0, tmp = p;
        for (int i = 0; i < n; i++) {
            if (tmp >= a[i])
                tmp -= a[i], res = i + 1;
            else
                break;
            if (i + k - 1 < n && tmp + a[i] >= a[i + k - 1]) cnt = i + k;
            if(cnt == n) break;
        }
        cout << max(res, cnt) << "\n";
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = upper_bound(a, a + n, p - sum) - a - 1;
            if(pos == n)
                pos = -1;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

ll sum[N];

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        sort(a + 1, a + n + 1);
        for (int i = 1; i <= n; i++)
            sum[i] = sum[i - 1] + a[i];
        ll ans = 0;
        for (int i = 0; i < k; i++) {
            ll cas = sum[i];
            if (cas > p)
                break;
            ll cnt = i;
            for (int j = i + k; j <= n; j += k){
                cas += a[j];
                if(cas > p)
                    break;
                cnt += k;
            }
            ans = max(ans, cnt);
        }
        cout << ans << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = upper_bound(a, a + n, p - sum) - a - 1;
            if(pos == n-1)
                pos = -1;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = lower_bound(a, a + n, p - sum) - a;
            if (pos != n && a[pos] == p - sum)
                pos += upper_bound(a, a + n, p - sum) - a - pos - 1;
            else
                pos--;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = lower_bound(a, a + n, p - sum) - a;
            if (a[pos] == p - sum && pos != n)
                pos += upper_bound(a, a + n, p - sum) - a - pos - 1;
            else
                pos--;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = cnt + k;
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int inf88 = 1482184792;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double pi = acos(-1.0);
typedef pair<int, int> P;
#define N 200100

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

    int t;
    cin >> t;
    while (t--) {
        ll n, p, k, a[N];
        cin >> n >> p >> k;
        for (int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        ll sum = 0;
        ll cnt = 0;
        ll ans = 0;
        for (int i = 0; i < n; i++) {
            ll pos = upper_bound(a, a + n, p - sum) - a - 1;
            // cout << "pos = " << pos << endl;
            if (pos - i + 1 >= k)
                ans = max(ans,cnt + k);
            sum += a[i];
            if (sum > p)
                break;
            cnt++;
            // cout << "ans = " << ans << endl;
            pos = upper_bound(a, a + n, p - sum) - a - 1;
            if (pos - i >= k)
                ans = max(ans,cnt + k);
        }
        cout << max(ans, cnt) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pll pair<ll,ll>
#define vi vector < int >
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define s(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define flash ios_base::sync_with_stdio(false);cin.tie(0)
#define goog(x) cout << "Case #" << x << ": "
#define MAX5 200005
#define MAX6 2000005
#define MLOG 18
#define limit 1e18
#define mod 1000000007
void __print(int x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int power(int x, int y, int p){
    int res = 1;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

int v[MAX5];

int f(int mid, int p, int k){
    multiset < int > s;
    for(int i = 0; i < mid; i++){
        s.insert(v[i]);
    }
    int ans = 0;
    while(s.size()){
        auto it = (s.end());
        it--;
        int x = *it;
        if(p >= x){
            int t = k;
            if(s.size() >= k){
                while(s.size() && t){
                    ans++;
                    t--;
                    s.erase((--s.end()));
                }
                p -= x;
            }
            else{
                break;
            }
        }
        else{
            break;
        }
    }
    //debug(mid, ans, p);
    while(s.size()){
        auto it = s.begin();
        int x = *it;
        if(p >= x){
            ans++;
            p -= x;
            s.erase(s.begin());
        }
        else{
            break;
        }
    }
    if(ans < mid) return 0;
    return 1;
}

void solve(){
    int n, p, k;
    cin >> n >> p >> k;
    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);
    int low = k;
    int high = n;
    int ans = 0;
    while(low <= high){
        int mid = (low+high)/2;
        if(f(mid, p , k)){
            ans = max(ans, mid);
            low = mid+1;
        }
        else{
            high = mid-1;
        }
    }
    int tp = p;
    int tans = 0;
    for(int i = 0 ; i < n; i++){
        if(v[i] <= tp){
            tans++;
            tp -= v[i];
        }
    }
    ans = max(ans, tans);
    int fans = 0;
    for(int i = 0; i < k; i++){
        tans = 0;
        tp = p;
        int j = i;
        while(j < n){
            if(j+k-1 < n){
                int s = v[j+k-1];
                if(tp >= s){
                    tans += k;
                    tp -= s;
                }
                else{
                    break;
                }
            }
            else{
                break;
            }
            j += k;
        }
        for(int a = 0; a < i; a++){
            if(tp >= v[a]){
                tans++;
                tp -= v[a];
            }
            else break;
        }
        for(j; j < n; j++){
            if(tp >= v[j]){
                tans++;
                tp -= v[j];
            }
            else break;
        }
        ans = max(ans, tans);
    }
    cout << max(ans, tans) << endl;
}
signed main(){
//  freopen("ii.txt", "w", stdin);
    flash;
    int t = 1;
     cin >> t;
    for(int tc = 1; tc <= t; tc++){
    //goog(tc);
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pll pair<ll,ll>
#define vi vector < int >
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define s(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define flash ios_base::sync_with_stdio(false);cin.tie(0)
#define goog(x) cout << "Case #" << x << ": "
#define MAX5 200005
#define MAX6 2000005
#define MLOG 18
#define limit 1e18
#define mod 1000000007
void __print(int x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int power(int x, int y, int p){
    int res = 1;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

int v[MAX5];

int f(int mid, int p, int k){
    multiset < int > s;
    for(int i = 0; i < mid; i++){
        s.insert(v[i]);
    }
    int ans = 0;
    while(s.size()){
        auto it = (s.end());
        it--;
        int x = *it;
        if(p >= x){
            int t = k;
            if(s.size() >= k){
                while(s.size() && t){
                    ans++;
                    t--;
                    s.erase((--s.end()));
                }
                p -= x;
            }
            else{
                break;
            }
        }
        else{
            break;
        }
    }
    //debug(mid, ans, p);
    while(s.size()){
        auto it = s.begin();
        int x = *it;
        if(p >= x){
            ans++;
            p -= x;
            s.erase(s.begin());
        }
        else{
            break;
        }
    }
    if(ans < mid) return 0;
    return 1;
}

void solve(){
    int n, p, k;
    cin >> n >> p >> k;
    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);
    int low = k;
    int high = n;
    int ans = 0;
    while(low <= high){
        int mid = (low+high)/2;
        if(f(mid, p , k)){
            ans = max(ans, mid);
            low = mid+1;
        }
        else{
            high = mid-1;
        }
    }
    int tp = p;
    int tans = 0;
    for(int i = 0 ; i < n; i++){
        if(v[i] <= tp){
            tans++;
            tp -= v[i];
        }
    }
    ans = max(ans, tans);
    vi vv(n+1, 0);
    int fans = 0;
    for(int i = 0; i < k; i++){
        tans = 0;
        tp = p;
        int j = i;
        while(j < n){
            if(j+k-1 < n){
                int s = v[j+k-1];
                if(p >= s){
                    tans += k;
                }
                else{
                    break;
                }
            }
            else{
                break;
            }
            j += k;
        }
        ans = max(ans, tans);
    }
    cout << max(ans, tans) << endl;
}
signed main(){
//  freopen("ii.txt", "w", stdin);
    flash;
    int t = 1;
     cin >> t;
    for(int tc = 1; tc <= t; tc++){
    //goog(tc);
        solve();
    }
}


#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define pll pair<ll,ll>
#define vi vector < int >
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define s(x) scanf("%d",&x)
#define sll(x) scanf("%lld",&x)
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define flash ios_base::sync_with_stdio(false);cin.tie(0)
#define goog(x) cout << "Case #" << x << ": "
#define MAX5 200005
#define MAX6 2000005
#define MLOG 18
#define limit 1e18
#define mod 1000000007
void __print(int x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

int power(int x, int y, int p){
    int res = 1;
    x = x % p;
    while (y > 0){
        if (y & 1)
            res = (res*x) % p;
        y = y>>1;
        x = (x*x) % p;
    }
    return res;
}

int v[MAX5];

void solve(){
    int n, p, k;
    cin >> n >> p >> k;
    for(int i = 0; i < n; i++) cin >> v[i];
    sort(v, v+n);
    int low = k;
    int high = n;
    int ans = 0;
    int tp = p;
    int tans = 0;
    for(int i = 0 ; i < n; i++){
        if(v[i] <= tp){
            tans++;
            tp -= v[i];
        }
    }
    ans = max(ans, tans);

    for(int i = 0; i < k; i++){
        tans = 0;
        tp = p;
        int j = i;
        while(j < n){
            if(j+k-1 < n){
                int s = v[j+k-1];
                if(tp >= s){
                    tans += k;
                    tp -= s;
                }
                else{
                    break;
                }
            }
            else{
                break;
            }
            j += k;
        }
        for(int a = 0; a < i; a++){
            if(tp >= v[a]){
                tans++;
                tp -= v[a];
            }
            else break;
        }
        for(j; j < n; j++){
            if(tp >= v[j]){
                tans++;
                tp -= v[j];
            }
            else break;
        }
        ans = max(ans, tans);
    }
    cout << ans << endl;
}
signed main(){
//  freopen("ii.txt", "w", stdin);
    flash;
    int t = 1;
     cin >> t;
    for(int tc = 1; tc <= t; tc++){
    //goog(tc);
        solve();
    }
}