#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char* argv[]) {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> vec = vector<int>(n);
    for (int j = 0; j < n; j++) {
      cin >> vec[j];
    }
    sort(vec.begin(), vec.end());
    auto count = vector<int>(k);
    auto _p = vector<int>(k);
    for (int j = 0; j < k; j++) {
      _p[j] = p;
    }
    int sum = 0;
    for (int j = 0; j < n; j++) {
      if (j < k) {
      }
      if (j < k - 1) {
        sum += vec[j];
        if (_p[j] - sum >= 0) {
          _p[j] -= sum;
          count[j % k] += j < k - 1 ? j + 1 : k;
        }
      } else {
        if (_p[j % k] - vec[j] >= 0) {
          _p[j % k] -= vec[j];
          count[j % k] += j < k - 1 ? j + 1 : k;
        }
      }
    }
    int maxVal = 0;
    for (int j = 0; j < k; j++) {
      maxVal = max(count[j], maxVal);
    }
    cout << maxVal << endl;
  }
  return 0;
}
