#include <bits/stdc++.h>
using namespace std;
const int INF = INT_MAX;
const long long limit = 1LL * 1e18;
const int MOD = 1e9 + 711;
const int MOD2 = 1e9 + 933;
const int mod = 1e9 + 7;
const int maxn = 1e4 + 100;
int n, p, k;
void solve() {
  cin >> n >> p >> k;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  vector<int> dp(n + 1);
  for (int i = 1; i <= n; ++i) {
    if (i >= k) {
      dp[i] = dp[i - k] + a[i];
    } else {
      dp[i] = dp[i - 1] + a[i];
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (dp[i] <= p) {
      ans = i;
    }
  }
  cout << ans << '\n';
}
signed main() {
  cout.tie(0);
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
