#include <bits/stdc++.h>
using namespace std;
const long long M = 4e5 + 10;
long long t, n, k, p, a[M], dp[M], s[M], ans;
long long bs(long long r, long long rem) {
  long long l = 0, mid;
  r++;
  while (r - l > 1) {
    mid = (r + l) / 2;
    if (s[mid] > rem)
      r = mid;
    else
      l = mid;
  }
  return l;
}
int32_t main() {
  cin >> t;
  while (t--) {
    scanf("%lld%lld%lld", &n, &p, &k);
    for (long long i = 1; i <= n; i++) {
      scanf("%lld", a + i);
    }
    sort(a + 1, a + n + 1);
    fill(s, s + n + 1, 0);
    fill(dp, dp + n + 1, 0);
    ans = 0;
    for (long long i = 1; i <= n; i++) s[i] = s[i - 1] + a[i];
    for (long long i = k; i < k + k; i++) dp[i] = a[i];
    for (long long i = k + k; i <= n; i++) dp[i] = dp[i - k] + a[i];
    for (long long i = n; i >= k; i--)
      if (dp[i] <= p) ans = max(ans, i - (i % k) + bs(i % k, p - dp[i]));
    ans = max(ans, bs(k - 1, p));
    cout << ans << endl;
  }
  return 0;
}
