#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
inline long long read() {
  long long w = 1, data = 0;
  char ch = 0;
  while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
  if (ch == '-') w = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') data = data * 10 + ch - '0', ch = getchar();
  return w * data;
}
long long T, n, p, k;
long long a[MAXN], f[MAXN];
int main() {
  T = read();
  for (long long t = 1; t <= T; t++) {
    n = read();
    p = read();
    k = read();
    for (long long i = 1; i <= n; i++) a[i] = read();
    sort(a + 1, a + n + 1);
    long long ans = 0;
    for (long long i = 1; i < k; i++) {
      f[i] = f[i - 1] + a[i];
      if (f[i] <= p) ans = i;
    }
    for (long long i = k; i <= n; i++) {
      f[i] = f[i - k] + a[i];
      if (f[i] <= p) ans = i;
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
