#include <bits/stdc++.h>
using namespace std;
long long dp[200001];
int main() {
  long long i, j, t, n, m, p, k;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    vector<long long> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    sort(v.begin(), v.end());
    int ans = 0;
    for (int i = 0; i < k - 1; i++) {
      dp[i] = v[i] + dp[i - 1];
      if (dp[i] <= p) ans = i + 1;
    }
    for (int i = k - 1; i < n; i++) {
      dp[i] = v[i] + dp[i - k];
      if (dp[i] <= p) ans = i + 1;
    }
    cout << ans << '\n';
  }
  return 0;
}
