#include <bits/stdc++.h>
using namespace std;
long long int pref[200001];
int main() {
  long long int t;
  cin >> t;
  for (long long int q = 0; q < t; q++) {
    long long int n, p, k;
    cin >> n >> p >> k;
    vector<long long int> v;
    for (long long int i = 0; i < n; i++) {
      long long int x;
      cin >> x;
      v.push_back(x);
    }
    sort(v.begin(), v.end());
    long long int sum = 0;
    long long int ans = 0;
    for (long long int start = 0; start <= k; start++) {
      long long int curr = sum;
      if (curr > p) break;
      long long int x = start;
      for (long long int j = start + (k - 1); j < n; j += k) {
        if (curr + v[j] <= p) {
          curr += v[j];
          x += k;
        } else
          break;
      }
      ans = max(ans, x);
      sum += v[start];
    }
    cout << ans << "\n";
  }
  return 0;
}
