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