#include <bits/stdc++.h>
using namespace std;
const int max6 = 1e6 + 3;
const int oo = 1e9 + 9;
int f[max6], a[max6];
int main() {
  int tests;
  scanf("%d", &tests);
  while (tests--) {
    int n, p, k;
    scanf("%d %d %d", &n, &p, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) f[i] = oo;
    f[0] = 0;
    for (int i = 1; i <= n; ++i) {
      f[i] = min(f[i], f[i - 1] + a[i]);
      if (i - k >= 0) f[i] = min(f[i], f[i - k] + a[i]);
    }
    int res = 0;
    for (int i = 1; i <= n; ++i)
      if (f[i] <= p) res = max(res, i);
    printf("%d\n", res);
  }
}
