#include <bits/stdc++.h>
using namespace std;
const long long M = 1000000007;
const long long N = 3e5 + 5;
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL);
  ;
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n, i, j, p, k;
    cin >> n >> p >> k;
    long long a[N], sum = 0, dp[N] = {};
    for (i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    for (i = 1; i <= n; i++) {
      dp[i] = dp[i - 1] + a[i];
      if (i >= k) dp[i] = min(dp[i], dp[i - k] + a[i]);
    }
    long long ans = 0;
    for (i = 1; i <= n; i++) {
      if (dp[i] <= p) ans = i;
    }
    cout << ans << "\n";
  }
}
