#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, m, a, i, b, j, k, t, x, y, z, tc, u, v, w;
  cin >> tc;
  while (tc--) {
    long long arr[200005], dp[200005];
    cin >> n >> x >> k;
    for (i = 0; i < n; i++) cin >> arr[i];
    sort(arr, arr + n);
    long long ans = 0;
    for (i = 0; i < n; i++) {
      if (i == 0)
        dp[i] = arr[i];
      else if (i < (k - 1))
        dp[i] = dp[i - 1] + arr[i];
      else if (i == k - 1)
        dp[i] = arr[i];
      else
        dp[i] = dp[i - k] + arr[i];
      if (dp[i] <= x) ans = max(ans, i + 1);
    }
    cout << ans << endl;
  }
}
