#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long iii = 0; iii < t; iii++) {
    long long n, p, k;
    long long sum = 0;
    cin >> n >> p >> k;
    long long a[n];
    long long pref = 0, res = 0;
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    sort(a, a + n);
    for (long long i = 0; i <= k; i++) {
      long long sum = pref;
      if (sum > p) break;
      long long sch = i;
      for (long long j = i + k - 1; j < n; j += k) {
        if (sum + a[j] <= p) {
          sch += k;
          sum += a[j];
        } else
          break;
      }
      pref += a[i];
      res = max(res, sch);
    }
    cout << res << endl;
  }
  return 0;
  ;
}
