#include <bits/stdc++.h>
using namespace std;
long long n, m, k, T, tc, ss, ans, po, a[200005], s[200005], v[200005],
    u[200005], d[200005];
int main() {
  long long i, j, z, num;
  cin >> T;
  for (tc = 1; tc <= T; tc++) {
    scanf("%I64d %I64d %I64d", &n, &m, &k);
    ss = 0;
    ans = 0;
    for (i = 0; i < n; i++) {
      scanf("%I64d", &a[i]);
    }
    sort(a, a + n);
    for (i = 0; i < n; i++) {
      ss += a[i];
      if (ss <= m) ans = i + 1;
    }
    u[0] = a[0];
    for (i = 1; i < n; i++) {
      u[i] = u[i - 1] + a[i];
    }
    po = 0;
    for (i = k - 1; i < n; i++) {
      z = i % k;
      if (v[z] != tc) {
        v[z] = tc;
        s[z] = 0;
      }
      s[z] += a[i];
      num = s[z];
      if (z != k - 1) num += u[z];
      if (num <= m) {
        ans = max(ans, i + 1);
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
