#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  for (long long int tc = 1; tc <= t; tc++) {
    long long int n, p, k;
    cin >> n >> p >> k;
    vector<long long int> v(n);
    for (long long int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    long long int sum = 0, prefix = 0, ans = -1;
    for (long long int i = 0; i <= k; i++) {
      sum = prefix;
      if (sum > p) break;
      long long int cnt = i;
      for (long long int j = i + k - 1; j < n; j += k) {
        if (sum + v[j] <= p) {
          cnt += k;
          sum += v[j];
        } else
          break;
      }
      ans = max(ans, cnt);
      prefix += v[i];
    }
    cout << ans << endl;
  }
}
