#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 123;
const int M = 1e9 + 7;
const int K = 500;
int n, p, k;
int a[N];
int b[N];
void solve() {
  cin >> n >> p >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    b[i] = b[i - 1] + a[i];
  }
  int ans = 0;
  for (int i = 1; i <= k; ++i) {
    int q = p;
    int lol = 0;
    int d = 0;
    for (int j = i + k - 1; j <= n; j += k) {
      if (q >= a[j]) {
        q -= a[j];
        d++;
        lol += k;
      }
    }
    int l = 1, r = i - 1;
    if (d == 0) {
      r = n;
    }
    int ok = 0;
    while (l <= r) {
      int m = (l + r) / 2;
      if (b[m] <= q) {
        ok = m;
        l = m + 1;
      } else {
        r = m - 1;
      }
    }
    lol += ok;
    ans = max(ans, lol);
  }
  cout << ans << '\n';
}
int main() {
  ios_base ::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int q;
  cin >> q;
  while (q--) solve();
  return 0;
}
