#include <bits/stdc++.h>
using namespace std;
typedef long long ll, *llp;
int t;
ll n, p, k, a[200010], sum, r, ans, l;
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld%lld", &n, &p, &k);
    for (ll i = 0; i < n; i++) {
      scanf("%lld", &a[i]);
    }
    sort(a, a + n);
    ans = 0;
    l = 0;
    for (ll i = k - 1; i < 2 * k - 1; i++) {
      sum = 0;
      r = 0;
      for (ll j = i; j < n; j += k) {
        if (sum + a[j] > p)
          break;
        else {
          sum += a[j];
          r += k;
        }
      }
      if (i == k - 1)
        l = r;
      else if (r < l)
        break;
      for (ll j = 0; j < i - k + 1 && j < n; j++) {
        if (sum + a[j] > p)
          break;
        else {
          sum += a[j];
          r++;
        }
      }
      ans = max(ans, r);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
