#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 200100;
const long long INF = 2000000100;
const long long MOD = 998244353;
int T, N, P, K, A[MAXN], S[MAXN];
int main() {
  ios_base::sync_with_stdio(0);
  cin >> T;
  for (int t = 0; t < T; t++) {
    cin >> N >> P >> K;
    A[0] = S[0] = 0;
    for (int i = 1; i <= N; i++) {
      cin >> A[i];
    }
    sort(A, A + N + 1);
    for (int i = 1; i <= N; i++) S[i] = S[i - 1] + A[i];
    int ans = 0;
    int p1, p2;
    for (int m = 0; m < K; m++) {
      p1 = S[m];
      p2 = 0;
      if (p1 <= P) {
        ans = max(ans, m);
        for (int i = 1; i * K + m <= N; i++) {
          p2 += A[i * K + m];
          if (p1 + p2 <= P) ans = max(ans, i * K + m);
        }
      }
    }
    cout << ans << "\n";
  }
}
