#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    long long a[n];
    for (long long i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    long long ans = 0, x = 0;
    for (long long i = 0; i < k; i++) {
      long long ca = i;
      long long cp = p - x;
      for (long long j = i + k - 1; j < n; j += k) {
        if (a[j] > cp) break;
        ca += k;
        cp -= a[j];
      }
      if (cp >= 0) ans = max(ans, ca);
      x += a[i];
    }
    cout << ans << '\n';
  }
  cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
  return 0;
}
