#include <bits/stdc++.h>
using namespace std;
long long int gcd(int a, int b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long int bitfun(long long int x) { return (x & (-x)); }
void solve() {
  long long int n, p, k;
  cin >> n >> p >> k;
  vector<long long int> a(n);
  for (int i = 0; i < a.size(); i++) cin >> a[i];
  ;
  long long int buy = 0;
  sort(a.begin(), a.end());
  long long int ans = -1e18;
  for (int i = 0; i < k + 1; i++) {
    long long int sm = buy, cnt = i;
    for (long long int j = k - 1 + i; j < n; j += k) {
      if (sm + a[j] <= p) {
        sm += a[j];
        cnt += k;
      } else
        break;
    }
    if (sm > p) break;
    ans = max(cnt, ans);
    buy += a[i];
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
}
