#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
void fio() {}
void pti() {
  double timeuse = clock() * 1000.0 / CLOCKS_PER_SEC;
  cerr << "Timeuse " << timeuse << "ms" << endl;
}
void end() {
  pti();
  exit(0);
}
namespace io {
const int SIZ = 55;
int que[SIZ], op, qr;
char ch;
template <class I>
inline void gi(I &w) {
  ch = getchar(), op = 1, w = 0;
  while (!isdigit(ch)) {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    w = w * 10 + ch - '0';
    ch = getchar();
  }
  w *= op;
}
template <class I>
inline void print(I w) {
  qr = 0;
  if (!w) putchar('0');
  if (w < 0) putchar('-'), w = -w;
  while (w) que[++qr] = w % 10 + '0', w /= 10;
  while (qr) putchar(que[qr--]);
}
}  // namespace io
using io::gi;
using io::print;
const int N = 2e5 + 5;
int T, n, p, k;
int a[N];
int main() {
  fio();
  gi(T);
  while (T--) {
    int ans = 0;
    gi(n), gi(p), gi(k);
    for (int i = 1; i <= n; ++i) gi(a[i]);
    sort(a + 1, a + n + 1);
    int pre = 0;
    for (int i = 0; i <= k; ++i) {
      if (pre > p) break;
      int sum = pre, cnt = i;
      for (int j = i + k; j <= n; j += k) {
        sum += a[j];
        if (sum > p) break;
        cnt += k;
      }
      ans = max(ans, cnt);
      pre += a[i + 1];
    }
    print(ans), putchar(10);
  }
  end();
}
