#include <bits/stdc++.h>
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char *x) { cerr << '\"' << x << '\"'; }
void __print(const string &x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x) cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
long long n, p, k;
vector<long long> arr(1000000);
bool fun(long long ind, long long pr) {
  long long cp = pr;
  long long i = ind - 1;
  while (i >= 0) {
    if (i >= k - 1) {
      if (cp >= arr[i]) {
        cp -= arr[i];
        i -= k;
      } else {
        return false;
      }
    } else {
      if (cp >= arr[i]) {
        cp -= arr[i];
        i -= 1;
      } else {
        return false;
      }
    }
  }
  return true;
}
int main() {
  long long tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> p >> k;
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
    };
    sort(arr.begin(), arr.begin() + n);
    long long lf = 0, rt = n / k, mid, ans = 0;
    while (lf <= rt) {
      mid = lf + (rt - lf) / 2;
      bool tmp = fun(mid * k + n % k, p);
      if (tmp == true) {
        ans = mid * k + n % k;
        lf = mid + 1;
      } else {
        rt = mid - 1;
      }
    }
    lf = 0, rt = n;
    while (lf <= rt) {
      mid = lf + (rt - lf) / 2;
      bool tmp = fun(mid, p);
      if (tmp == true) {
        ans = max(mid, ans);
        lf = mid + 1;
      } else {
        rt = mid - 1;
      }
    }
    long long tmp1 = 0;
    for (long long i = 0; i < n; i++) {
      if (tmp1 + arr[i] <= p) {
        tmp1 += arr[i];
        ans = max(ans, i + 1);
      }
    }
    cout << ans << endl;
  }
}
