#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long T;
  scanf("%lld", &T);
  while (T--) {
    long long n, p, k;
    scanf("%lld%lld%lld", &n, &p, &k);
    vector<long long> A(n);
    vector<long long> D(n);
    vector<long long> S(n);
    for (long long i = 0; i < n; ++i) scanf("%lld", &A[i]);
    sort(A.begin(), A.end());
    S[0] = A[0];
    for (long long i = 1; i < n; ++i) S[i] = S[i - 1] + A[i];
    D[0] = A[0];
    long long res = (D[0] <= p ? 1 : 0);
    for (long long i = 1; i < n; ++i) {
      D[i] = D[i - 1] + A[i];
      if (i - k >= 0) D[i] = min(D[i], D[i - k] + A[i]);
      if (i - k == -1) D[i] = min(D[i], A[i]);
      if (p >= D[i]) res = i + 1;
    }
    printf("%lld\n", res);
  }
  return 0;
}
