#include <bits/stdc++.h>
using namespace std;
struct data {
  long x;
  long y;
};
long a[200005];
struct data b[200005];
bool cmp(long x, long y) { return x < y; }
int main() {
  int t;
  cin >> t;
  b[0].x = 0;
  b[0].y = 0;
  while (t--) {
    long n, p, k;
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
      b[i].x = 0;
      b[i].y = 0;
    }
    sort(a + 1, a + n + 1, cmp);
    long ans = 0;
    for (int i = 1; i < k; i++) {
      if (b[i - 1].x + a[i] <= p) {
        b[i].x = b[i - 1].x + a[i];
        b[i].y = b[i - 1].y + 1;
        ans = max(b[i].y, ans);
      }
    }
    for (int i = k; i <= n; i++) {
      int ans1 = 0;
      if (b[i - k].x + a[i] <= p) {
        b[i].x = b[i - k].x + a[i];
        b[i].y = b[i - k].y + k;
        ans = max(b[i].y, ans);
      } else {
        if (b[i - 1].x + a[i] <= p) {
          b[i].x = b[i - 1].x + a[i];
          b[i].y = b[i - 1].y + 1;
          ans = max(b[i].y, ans);
        }
      }
    }
    cout << ans << endl;
  }
}
