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