#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k, t;
    cin >> n >> p >> k;
    vector<int> v;
    for (int ctr1 = 0; ctr1 < n; ++ctr1) cin >> t, v.push_back(t);
    sort(v.begin(), v.end());
    int dp[n + 1];
    memset(dp, 0, sizeof(dp));
    for (int ctr1 = 0; ctr1 < n; ++ctr1)
      dp[ctr1 + 1] = v[ctr1] + (ctr1 >= k ? dp[ctr1 + 1 - k] : 0);
    int tp = p, rez = 0;
    for (int ctr1 = 0; ctr1 < n; ++ctr1) {
      long long l = 0, r = n;
      while (l <= r) {
        long long mid = l + (r - l) / 2;
        if (ctr1 + mid * k <= n && tp >= dp[ctr1 + mid * k] - dp[ctr1]) {
          rez = max(rez, (int)mid * k + ctr1);
          l = mid + 1;
        } else {
          r = mid - 1;
        }
      }
      tp -= v[ctr1];
    }
    cout << rez << "\n";
  }
  return 0;
}
