#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int l[maxn];
int dp[maxn];
void solve() {
  int n, p, k;
  cin >> n >> p >> k;
  for (int i = 0; i < n; i++) {
    cin >> l[i];
  }
  sort(l, l + n);
  for (int i = 1; i <= n; i++) {
    dp[i] = dp[i - 1] + l[i - 1];
    if (i - k >= 0) {
      dp[i] = min(dp[i], dp[i - k] + l[i - 1]);
    }
  }
  for (int i = n; i > -1; i--) {
    if (dp[i] <= p) {
      cout << i << '\n';
      return;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
