#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int T, n, p, k, a[N], maxn, ans, now, num, pre;
int main() {
  scanf("%d", &T);
  while (T--) {
    maxn = ans = now = pre = 0;
    scanf("%d%d%d", &n, &p, &k);
    for (int i = 0; i < n; i++) scanf("%d", &a[i]), maxn += a[i];
    sort(a, a + n);
    if (p < a[0]) {
      puts("0");
      continue;
    }
    if (p > maxn) {
      printf("%d\n", n);
      continue;
    }
    for (int i = 0; i < k; i++) {
      now = pre, num = i;
      if (p < now) break;
      for (int j = i + k - 1; j < n; j += k) {
        now += a[j];
        if (p >= now)
          num += k;
        else
          break;
      }
      pre += a[i];
      ans = max(ans, num);
    }
    printf("%d\n", ans);
  }
  return 0;
}
