#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e18;
const long long MAXN = 2e5 + 100;
long long T;
long long N, P, K;
long long A[MAXN];
long long dp[MAXN];
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> T;
  while (T--) {
    cin >> N >> P >> K;
    for (long long i = 0; i <= N; i++) dp[i] = oo;
    for (long long i = 1; i <= N; i++) {
      cin >> A[i];
    }
    sort(A + 1, A + N + 1);
    dp[0] = 0;
    long long res = 0;
    for (long long i = 1; i <= N; i++) {
      dp[i] = dp[i - 1] + A[i];
      if (i - K >= 0) dp[i] = min(dp[i], dp[i - K] + A[i]);
      if (dp[i] <= P) res = max(res, i);
    }
    cout << res << '\n';
  }
  return 0;
}
