#include <bits/stdc++.h>
using namespace std;
int main() {
  long t;
  cin >> t;
  while (t--) {
    long n, p, k;
    cin >> n >> p >> k;
    long price[n];
    for (long i = 0; i < n; ++i) {
      cin >> price[i];
    }
    sort(price, price + n);
    long ans = 0;
    long prev = 0;
    for (long i = 0; i < k; i++) {
      long newp = p - prev;
      if (newp < 0) {
        break;
      }
      long tans = i;
      long idx = i + k - 1;
      while (newp > 0 && idx < n) {
        if (newp >= price[idx]) {
          newp -= price[idx];
          tans += k;
        } else {
          break;
        }
        idx += k;
      }
      idx = 0;
      ans = max(ans, tans);
      prev += price[i];
    }
    cout << ans << endl;
  }
  return 0;
}
