#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
const int INF = 0x3f3f3f3f, N = 2e5 + 5;
const ll LINF = 1e18 + 5;
int T, n, p, k, a[N];
ll sum[N];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> T;
  while (T--) {
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (i < k)
        sum[i] = sum[i - 1] + a[i];
      else
        sum[i] = sum[i - k] + a[i];
      if (sum[i] <= p) ans = i;
    }
    cout << ans << '\n';
  }
  return 0;
}
