#include <bits/stdc++.h>
using namespace std;
long long const N = 1000005, mod = 1000000007;
long long n, m, k, t, i, j, ans, kaskad[N], pref[N];
void solve() {
  long long n, p, k;
  cin >> n >> p >> k;
  for (i = 0; i < n; ++i) {
    cin >> kaskad[i];
    pref[i + 1] = 0;
  }
  sort(kaskad, kaskad + n);
  ans = 0;
  for (i = 0; i < k - 1; ++i) {
    pref[i + 1] = pref[i] + kaskad[i];
    if (pref[i + 1] <= p) {
      ans = i + 1;
    }
  }
  for (int i = k - 1; i < n; ++i) {
    pref[i + 1] = pref[i + 1 - k] + kaskad[i];
    if (pref[i + 1] <= p) {
      ans = i + 1;
    }
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}
