#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int pref = 0;
    int ans = 0;
    sort(a.begin(), a.end());
    for (int i = 0; i <= k; i++) {
      int sum = pref;
      if (sum > p) break;
      int cnt = i;
      for (int j = i + k - 1; j < n; j += k) {
        if (sum + a[j] <= p) {
          cnt += k;
          sum += a[j];
        } else
          break;
      }
      ans = max(cnt, ans);
      pref += a[i];
    }
    cout << ans << endl;
  }
  return 0;
}
