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