#include <bits/stdc++.h>
using namespace std;
const long double pi = 2 * acos(0.0);
template <class T>
bool umin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
bool umax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class TT>
bool pal(T a, TT n) {
  int k = 0;
  for (int i = 0; i <= n / 2; i++) {
    if (a[i] != a[n - i - 1]) {
      k = 1;
      break;
    }
  }
  return k ? 0 : 1;
}
long long a[222222], dp[222222];
void solve() {
  long long n, p, k;
  cin >> n >> p >> k;
  for (int i = 0; i <= n; i++) dp[i] = 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; i++) dp[i] = dp[i - 1] + a[i];
  long long ans = 0;
  for (int i = 0; i < k; i++) {
    if (dp[i] > p) break;
    ans = max(ans, (i * 1ll));
    long long s = dp[i];
    for (int j = k + i; j <= n; j += k) {
      if (s + a[j] <= p) {
        s += a[j];
        ans = max(ans, (j * 1ll));
      } else
        break;
    }
  }
  cout << ans << '\n';
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
  getchar();
  getchar();
  return 0;
}
