#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
int n, p, k;
int a[MAX_N], sum[MAX_N];
int main() {
  int T;
  scanf("%d", &T);
  a[0] = 0;
  while (T--) {
    scanf("%d", &n), scanf("%d", &p), scanf("%d", &k);
    for (register int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for (register int i = 1; i <= n; i++) {
      if (i >= k)
        sum[i] = a[i] + sum[i - k];
      else
        sum[i] = a[i] + sum[i - 1];
    }
    int ans = 0;
    for (register int i = 1; i <= n; i++)
      if (sum[i] <= p) ans = max(ans, i);
    printf("%d\n", ans);
  }
  return 0;
}
