#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n, p, k;
  cin >> n >> p >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  sort(v.begin(), v.end());
  vector<vector<int>> g(k, vector<int>());
  for (int i = 0; i < n; i++) g[i % k].push_back(v[i]);
  vector<vector<int>> ps(k, vector<int>());
  for (int i = 0; i < k; i++) {
    ps[i].resize(g[i].size());
    ps[i][0] = g[i][0];
    for (int j = 1; j < g[i].size(); j++) ps[i][j] = ps[i][j - 1] + g[i][j];
  }
  int cts = 0;
  while (cts < ps[k - 1].size() && ps[k - 1][cts] <= p) ++cts;
  int d = 0, c = 0;
  for (int t = 1; t < k; t++) {
    if (t + cts * k > n) break;
    if (c + v[t - 1] + ps[t - 1][cts] - ps[t - 1][0] > p) break;
    ++d;
    c += v[t - 1];
  }
  cout << (d + cts * k) << '\n';
}
int main(int argc, char** argv) {
  ios::sync_with_stdio(false), cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
