#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int kcase;
  cin >> kcase;
  while (kcase--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> v(n + 1, 0);
    for (int i = 1; i <= n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    vector<int> f(n + 1, 0);
    for (int i = 1; i <= n; i++)
      if (i < k)
        f[i] = f[i - 1] + v[i];
      else
        f[i] = min(f[i - k], f[i - 1]) + v[i];
    int cnt = 0;
    for (int i = 1; i <= n; i++)
      if (p >= f[i]) cnt = max(cnt, i);
    cout << cnt << endl;
  }
  return 0;
}
