#include <bits/stdc++.h>
using namespace std;
const int N = 300300;
int sum[N], a[N];
void solve() {
  int n, p, k;
  cin >> n >> p >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  for (int i = 1; i < k; i++) sum[i] = sum[i - 1] + a[i];
  for (int i = k; i <= n; i++) sum[i] = sum[i - k] + a[i];
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (sum[i] <= p) ans = i;
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    solve();
  }
  return (0);
}
