#include <bits/stdc++.h>
using namespace std;
long long a[200005];
long long cum[200005];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n, p, k;
    cin >> n >> p >> k;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    long long ans = 0;
    long long cnt = 0;
    long long temp = p;
    for (int i = k - 1; i < n; i += k) {
      if (a[i] <= temp) {
        temp -= a[i];
        cnt += k;
      } else
        break;
    }
    long long sum = 0;
    for (int i = 0; i < n; i++) {
      sum += a[i];
      cum[i] = sum;
    }
    ans = max(ans, cnt);
    int in = 0;
    while (in < k && (temp - a[in]) >= 0) {
      cnt = 0;
      temp = p;
      temp -= cum[in];
      if (temp < 0) break;
      for (int i = in + k; i < n; i += k) {
        if (a[i] <= temp) {
          temp -= a[i];
          cnt += k;
        } else
          break;
      }
      ans = max(ans, cnt + in + 1);
      in++;
    }
    cout << ans << endl;
  }
  return 0;
}
