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