#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  vector<long long> ans;
  for (int i = 0; i < t; i++) {
    long long n, p, k;
    cin >> n >> p >> k;
    vector<long long> arr;
    long long count = 0;
    arr.resize(n);
    for (int j = 0; j < n; j++) {
      cin >> arr[j];
    }
    sort(arr.begin(), arr.end());
    long long sum = 0;
    for (int j = 0; j < k - 1; j++) {
      long long temp = arr[j];
      arr[j] = arr[j] + sum;
      sum += temp;
    }
    for (int j = k; j < n; j++) {
      arr[j] += arr[j - k];
    }
    for (long long j = 0; j < n; j++) {
      if (arr[j] <= p) count = max(count, j + 1);
    }
    ans.push_back(count);
  }
  for (int i = 0; i < t; i++) {
    cout << ans[i] << "\n";
  }
  return 0;
}
