#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    int arr[n];
    int sum[n];
    for (int i = 0; i < n; i++) {
      cin >> arr[i];
    }
    sort(arr, arr + n);
    for (int i = 0; i < k; i++) {
      if (i == 0)
        sum[i] = arr[i];
      else
        sum[i] = arr[i] + arr[i - 1];
    }
    int mx = 0;
    int ans = 0;
    for (int i = 0; i < k; i++) {
      int amt = 0;
      ans = 0;
      for (int j = (i + k - 1); j < n; j += k) {
        if (amt + arr[j] <= p) {
          amt += arr[j];
          ans += k;
        } else {
          break;
        }
      }
      int hi = i - 1;
      int lo = 0;
      while (hi >= lo) {
        int mid = (hi + lo) / 2;
        if (amt + sum[mid] <= p)
          lo = mid + 1;
        else
          hi = mid - 1;
      }
      ans += hi + 1;
      if (ans > mx) mx = ans;
    }
    cout << mx << endl;
  }
  return 0;
}
