#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
mt19937 rnd;
vector<ll> lol(1e6, -1);
int main() {
  rnd = mt19937(12345);
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<ll> x(n);
    for (int i = 0; i < n; i++) cin >> x[i];
    sort(x.begin(), x.end());
    vector<ll> num(k);
    vector<ll> psum(n + 1);
    ll ma = 0;
    psum[0] = 0;
    for (int i = 0; i < n; i++) {
      psum[i + 1] = psum[i] + x[i];
    }
    for (int i = 0; i < k; i++) {
      ll curp = p;
      if (curp >= psum[i]) {
        num[i] += i;
        curp -= psum[i];
      }
      for (int j = i; j + k <= n; j += k) {
        if (x[j + k - 1] <= curp) {
          curp -= x[j + k - 1];
          num[i] += k;
        }
      }
      ma = max(ma, num[i]);
    }
    cout << ma << '\n';
  }
}
