#include <bits/stdc++.h>
using namespace std;
int static_init = []() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout << fixed;
  return 0;
}();
int main() {
  int t, n, p, k;
  vector<int16_t> a;
  cin >> t;
  while (--t >= 0) {
    cin >> n >> p >> k;
    a.resize(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a.begin(), a.end());
    int ans = 0, sum0 = 0;
    for (int i = 0; i < k && sum0 <= p; ++i) {
      int sum = sum0, sofar = i;
      for (int j = i + k - 1; j < n && sum + a[j] <= p; j += k) {
        sum += a[j];
        sofar += k;
      }
      ans = max(ans, sofar);
      sum0 += a[i];
    }
    cout << ans << '\n';
  }
  return 0;
}
