#include <bits/stdc++.h>
using namespace std;
long long int max(long long int a, long long int b) {
  if (a > b)
    return a;
  else
    return b;
}
long long int min(long long int a, long long int b) {
  if (a < b)
    return a;
  else
    return b;
}
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
int XX[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int YY[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int t, n, m, i, j, q, x, a, y, b, k, p;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    long long int arr[n];
    for (i = 0; i < n; i++) {
      cin >> arr[i];
    }
    sort(arr, arr + n);
    long long int pref[n];
    pref[0] = arr[0];
    for (i = 1; i < n; i++) pref[i] = arr[i] + pref[i - 1];
    long long int ans = 0;
    for (i = 0; i < k; i++) {
      if (arr[i] > p) break;
      long long int count = 0;
      if (i == k - 1)
        count += k;
      else
        count = 1;
      long long int sum = arr[i];
      j = i;
      while (j + k < n && sum + arr[j + k] <= p) {
        count += k;
        j += k;
        sum += arr[j];
      }
      long long int low = j + 1, high = min(n - 1, j + k), index = -1;
      if (i != k - 1) {
        low = 0, high = i - 1, index = -1;
        while (low <= high) {
          long long int mid = (low + high) >> 1;
          if (sum + pref[mid] <= p) {
            index = mid;
            low = mid + 1;
          } else
            high = mid - 1;
        }
        if (index != -1) {
          sum += pref[index];
          count += (index + 1);
        }
      }
      low = j + 1, high = n - 1, index = -1;
      while (low <= high) {
        long long int mid = (low + high) >> 1;
        if (sum + pref[mid] - pref[j] <= p) {
          index = mid;
          low = mid + 1;
        } else
          high = mid - 1;
      }
      if (index != -1) count += (index - j);
      ans = max(ans, count);
    }
    cout << ans << "\n";
  }
}
