#include <bits/stdc++.h>
using namespace std;
int n, m, ans, k, num[200010], sum[200010], sum2[200010];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%d%d%d", &n, &m, &k), ans = 0;
    for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
    sort(num + 1, num + n + 1);
    for (int i = 1; i <= n; i++) sum[i] = sum[max(0, i - k)] + num[i];
    for (int i = 1; i <= n; i++) sum2[i] = sum2[i - 1] + num[i];
    for (int i = 1; i <= n; i++) {
      if (sum2[i - 1] > m) break;
      int l = i, r = n + 1;
      while (l < r) {
        int mid = (l + r) >> 1;
        if (sum[((int)(mid - (i - 1)) / k) * k + (i - 1)] - sum[i - 1] +
                sum2[i - 1] >
            m)
          r = mid;
        else
          l = mid + 1;
      }
      l--, ans = max(ans, i - 1 + (int)((int)(l - i + 1) / k) * k);
    }
    printf("%d\n", ans);
  }
}
