#include <bits/stdc++.h>
using namespace std;
vector<long long> pos[200020];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long T;
  cin >> T;
  while (T--) {
    vector<long long> v;
    long long n, m, k;
    cin >> n >> m >> k;
    long long x;
    for (int i = 0; i < k; i++) pos[i].clear();
    for (long long i = 0; i < n; i++) {
      cin >> x;
      v.push_back(x);
    }
    sort(v.begin(), v.end());
    for (int i = k - 1; i < v.size(); i++) {
      pos[(i + 1) % k].push_back(v[i]);
    }
    long long sum = 0;
    vector<long long> sv;
    long long ans = 0;
    long long tot = 0;
    for (int i = 0; i < v.size(); i++) {
      tot += v[i];
      if (tot <= m) ans = i + 1;
    }
    for (int i = 0; i < k; i++) {
      long long cost = 0;
      long long cnt = 0;
      for (auto u : pos[i]) {
        cnt += k;
        cost += u;
        if (cost > m) break;
        long long rem = m - cost;
        long long add = upper_bound(sv.begin(), sv.end(), rem) - sv.begin();
        ans = max(cnt + add, ans);
      }
      sum += v[i];
      sv.push_back(sum);
    }
    cout << ans << "\n";
  }
}
