#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, MOD = 1e9 + 7;
long long a[N];
void solve() {
  long long n, p, k;
  cin >> n >> p >> k;
  for (long long i = 0; i < n; i++) cin >> a[i];
  sort(a, a + n);
  vector<long long> v(n + 1, -1);
  v[0] = p;
  v[1] = p - a[0];
  long long ans = 0;
  if (v[1] >= 0) ans = 1;
  for (long long i = 1; i < n; i++) {
    if (i + 1 >= k)
      v[i + 1] = v[max(0ll, i - (k - 1))] - a[i];
    else
      v[i + 1] = v[i] - a[i];
    if (v[i + 1] >= 0) {
      ans = i + 1;
    }
  }
  cout << ans << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  for (long long tt = 1; tt < t + 1; tt++) {
    solve();
  }
  return 0;
}
