#include <bits/stdc++.h>
using namespace std;
int t, n, p, k, result;
vector<int> a;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    a.resize(n);
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a.begin(), a.end());
    vector<int> dp(n);
    for (int i = 0; i < n; ++i) {
      if (i == 0 || i == k - 1) {
        dp[i] = a[i];
      } else if (i < k) {
        dp[i] = dp[i - 1] + a[i];
      } else {
        dp[i] = min(dp[i - 1], dp[i - k]) + a[i];
      }
    }
    for (int i = n - 1; i >= 0; --i) {
      if (p >= dp[i]) {
        result = i + 1;
        break;
      }
    }
    cout << result << "\n";
    result = 0;
    a.clear();
  }
  return 0;
}
