#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e6 + 123;
const int mod = (int)1e9 + 7;
const long long inf = 1e18 + 9;
inline void boost() {
  ios_base::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
}
int n, have, k, pref[N], a[N];
inline void solve() {
  memset(pref, 0, sizeof pref);
  cin >> n >> have >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + n + 1);
  int ans = 0;
  for (int i = 1; i <= k - 1; i++) pref[i] = pref[i - 1] + a[i];
  for (int i = k; i <= n; i++) {
    pref[i] = pref[i - k] + a[i];
    if (pref[i - k] + a[i] <= have) ans = max(ans, i);
  }
  for (int i = 1; i < k; i++) ans = max(ans, (pref[i] <= have ? i : 0));
  cout << ans << endl;
}
int main() {
  boost();
  int TT = 1;
  cin >> TT;
  while (TT--) solve();
  return 0;
}
