#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
void solve() {
  int N, K;
  long long P;
  cin >> N >> P >> K;
  vector<long long> A(N);
  for (int i = 0; i < (N); ++i) cin >> A[i];
  sort(A.begin(), A.end());
  int ans = 0;
  long long sum = 0;
  for (int i = 0; i < K; i++) {
    long long res = sum;
    if (res > P) break;
    int j = i + K - 1;
    for (; j < N; j += K) {
      if (res + A[j] > P) break;
      res += A[j];
    }
    ans = max(ans, i + K * ((j - i) / K));
    sum += A[i];
  }
  cout << ans << endl;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = 0; i < (T); ++i) solve();
  return 0;
}
