#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
const int MAXK = 30 + 10;
int n, p, k;
int v[MAXN], memo[MAXN], prefix[MAXK];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) cin >> v[i];
    sort(v + 1, v + n + 1);
    for (int i = 1; i < k; i++) prefix[i] = prefix[i - 1] + v[i];
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (i < k)
        memo[i] = prefix[i];
      else
        memo[i] = v[i] + memo[i - k];
      if (memo[i] <= p) ans = max(ans, i);
    }
    cout << ans << "\n";
  }
  return 0;
}
