#include <bits/stdc++.h>
using namespace std;
int64_t solve(const int64_t N, const int64_t Q, const int64_t K,
              const vector<int64_t>& A) {
  vector<int64_t> B(A);
  sort(B.begin(), B.end());
  vector<int64_t> rem_prefix(K + 1);
  for (int64_t i = 0; i < K; ++i) {
    rem_prefix[i + 1] = rem_prefix[i] + B[i];
  }
  int64_t best = 0;
  for (int64_t r = 0; r < K; ++r) {
    int64_t tot = rem_prefix[r];
    for (int64_t i = 0; i + r <= N; i += K) {
      if (r + i - 1 >= K - 1) {
        tot += B[r + i - 1];
      };
      if (tot <= Q) {
        best = max(best, r + i);
      } else {
        break;
      }
    }
  }
  return best;
}
int main() {
  ios_base::sync_with_stdio(false);
  const unsigned seed = chrono::system_clock::now().time_since_epoch().count();
  mt19937 gen(seed);
  int64_t T;
  cin >> T;
  ;
  for (; T-- > 0;) {
    int64_t N;
    cin >> N;
    ;
    int64_t Q;
    cin >> Q;
    ;
    int64_t K;
    cin >> K;
    ;
    vector<int64_t> A(N);
    {
      for (int64_t i = 0; i < N; ++i) {
        cin >> A[i];
      }
    };
    cout << solve(N, Q, K, A) << "\n";
  }
  return 0;
}
