#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using ld = long double;
using D = double;
using vi = vector<int>;
using vii = vector<ii>;
using vvi = vector<vi>;
using vs = vector<string>;
template <typename T>
T abs(T x) {
  return x < 0 ? -x : x;
}
template <typename T>
T sgn(T x) {
  return x < 0 ? -1 : (x > 0 ? 1 : 0);
}
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
T gcd(T a, T b) {
  return a ? gcd(b % a, a) : b;
}
int n;
int m;
int k;
int x[200000];
int main() {
  int tt;
  scanf("%d", &tt);
  for (int it = 0; it < tt; it++) {
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 0; i < n; i++) scanf("%d", &x[i]);
    sort(x, x + n);
    int ans = 0;
    for (int t = 0; t < k; t++) {
      int pos = t, mm = m;
      while (pos + k - 1 < n && mm >= x[pos + k - 1]) {
        mm -= x[pos + k - 1];
        pos += k;
      }
      ans = max(ans, pos);
      m -= x[t];
      if (m < 0) break;
    }
    printf("%d\n", ans);
  }
  return 0;
}
