#include <bits/stdc++.h>
using namespace std;
int rest(const vector<int>& acc, int p) {
  if (acc[0] > p) {
    return 0;
  }
  auto up = std::upper_bound(acc.begin(), acc.end(), p);
  return up - acc.begin();
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> prices(n);
    vector<int> acc(n);
    for (int i = 0; i < n; i++) {
      cin >> prices[i];
    }
    sort(prices.begin(), prices.end());
    acc[0] = prices[0];
    for (int i = 1; i < n; i++) {
      acc[i] = acc[i - 1] + prices[i];
    }
    if (acc.back() <= p) {
      cout << n << endl;
      continue;
    }
    int sumFirst = 0;
    int indexFirst = 0;
    for (int i = k - 1; i < n; i += k) {
      sumFirst += prices[i];
      if (sumFirst > p) {
        break;
      }
      indexFirst = i;
    }
    if (indexFirst == 0) {
      cout << rest(acc, p) << endl;
      continue;
    }
    int ans = indexFirst + 1;
    for (int ende = indexFirst + 1; ende < n && (ende - indexFirst) < k;
         ende++) {
      int start = ende;
      int money = p;
      for (; start >= 0; start -= k) {
        money -= prices[start];
        if (money < 0) {
          break;
        }
      }
      if (start < 0) {
        start += k;
      }
      if (money < 0) {
        break;
      }
      int tempAns = ende - start;
      if (start != k) {
        tempAns++;
      }
      int restAns = rest(acc, money);
      if (restAns < start) {
        tempAns += restAns;
      } else {
        tempAns += start;
      }
      ans = max(ans, tempAns);
    }
    cout << ans << endl;
  }
}
