#include <bits/stdc++.h>
using namespace std;
long long n, x, k, z, m, pos, q, ans_v, y;
long long x1, p1, x2;
long long a[200500], b[200500], c[200500];
vector<long long> f;
vector<long long> g[200500];
void solve() {
  long long p;
  cin >> n >> p >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i <= n; i++) {
    b[i] = c[i] = 0;
  }
  sort(a, a + n);
  for (int i = 0; i < n; i++) {
    c[i + 1] = c[i] + a[i];
    if (i < k) {
      b[i] = a[i];
    } else {
      b[i] = b[i - k] + a[i];
    }
  }
  long long x = 0;
  for (long long i = 0; i < n; i++) {
    if (b[i] <= p) {
      long long suck = (i + 1) / k * k;
      long long diff = i + 1 - suck;
      long long diffp = p - b[i];
      long long pos = upper_bound(c, c + diff, diffp) - c;
      x = max(x, suck + pos);
    }
  }
  cout << x << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
