#include <bits/stdc++.h>
using namespace std;
const long long inf = 100000000;
vector<long long> parent;
vector<long long> arr;
set<long long> kek;
void work() {
  long long n, k, p;
  cin >> n >> p >> k;
  arr.resize(n);
  for (long long i = 0; i < n; i++) cin >> arr[i];
  sort(arr.begin(), arr.end());
  long long maxic = 0;
  long long pref_sum = 0;
  for (long long i = 0; i < n; i++) {
    if (p < pref_sum) break;
    if (i == k) break;
    long long ost = p - pref_sum;
    long long col = 0;
    for (long long j = i + k - 1; j < n; j += k) {
      if (arr[j] <= ost) {
        ost -= arr[j];
        col += k;
      } else
        break;
    }
    pref_sum += arr[i];
    maxic = max(maxic, i + col);
  }
  cout << maxic << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  for (long long i = 0; i < t; i++) {
    work();
  }
  return 0;
}
