#include <bits/stdc++.h>
using namespace std;
const int N = 5 + 2e5;
int a[N], p;
pair<int, int> ans[N];
pair<int, int> best(pair<int, int> a, pair<int, int> b) {
  if (a > b) swap(a, b);
  if (b.first > p) {
    if (a.first <= p) return a;
    return {0, 0};
  } else {
    if (b.second > a.second) return b;
    return a;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, t, k;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a, a + n);
    for (int i = 0; i < n; ++i) {
      ans[i] = best({a[i], 1}, {a[i], 1});
      if (i > 0)
        ans[i] = best(ans[i], {a[i] + ans[i - 1].first, 1 + ans[i - 1].second});
      if (i == k - 1) ans[i] = best(ans[i], {a[i], k});
      if (i >= k)
        ans[i] = best(ans[i], {a[i] + ans[i - k].first, k + ans[i - k].second});
    }
    int ANS = -1;
    for (int i = 0; i < n; ++i) ANS = max(ANS, ans[i].second);
    cout << ANS << "\n";
  }
}
