#include <bits/stdc++.h>
using namespace std;
int t;
int a[200005];
int sums[200005];
void test_case() {
  int n, p, k;
  cin >> n >> p >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    sums[1 + i] = sums[i] + a[i];
  }
  int cnt_res = 0;
  for (int w = 0; w < k; w++) {
    int res = sums[w];
    if (res > p) continue;
    cnt_res = max(cnt_res, w);
    int broke_point = -1;
    for (int j = w + k - 1; j < n; j += k) {
      res += a[j];
      if (res > p) {
        res -= a[j];
        break;
      }
      broke_point = j;
      cnt_res = max(cnt_res, j + 1);
    }
    if (broke_point < 0) continue;
    auto k_ind =
        lower_bound(sums, sums + 1 + n, sums[broke_point + 1] + p - res);
    int loc = (k_ind - sums);
    cnt_res = max(cnt_res, loc - 1);
  }
  cout << cnt_res << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> t;
  while (t--) test_case();
  return 0;
}
