#include <bits/stdc++.h>
using namespace std;
const long long int MOD = 1000000007;
const long long BIG = 1446803456761533460;
const int Big = 336860180;
stringstream sss;
const long long int maxn = 200010;
long long int ar[maxn];
void MAIN() {
  long long int t;
  cin >> t;
  for (long long int tt = (0); tt < (t); ++tt) {
    long long int n, p, k;
    cin >> n >> p >> k;
    for (long long int i = (0); i < (n); ++i) cin >> ar[i];
    sort(ar, ar + n);
    long long int ans = 0;
    long long int P = p;
    for (long long int ii = (0); ii < (k); ++ii) {
      p = P;
      long long int cnt = ii;
      for (long long int j = (0); j < (ii); ++j) {
        p -= ar[j];
      }
      for (long long int i = k - 1 + ii; i < n; i += k) {
        if (p >= ar[i]) {
          p -= ar[i];
          cnt += k;
        }
      }
      if (p >= 0) ans = max(ans, (cnt));
    }
    cout << ans << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(10);
  sss << R"(
8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2
    )";
  MAIN();
  return 0;
}
