#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
      cin >> v[i];
    }
    sort(v.begin(), v.end());
    vector<int> dp(n);
    int res = -1;
    dp[0] = v[0];
    for (int i = 1; i < k - 1; i++) {
      dp[i] = dp[i - 1] + v[i];
    }
    dp[k - 1] = v[k - 1];
    for (int i = k; i < n; i++) {
      dp[i] = dp[i - k] + v[i];
    }
    for (int i = 0; i < n; i++) {
      if (dp[i] <= p) res = max(res, i);
    }
    if (res == -1)
      cout << "0" << '\n';
    else
      cout << res + 1 << '\n';
  }
}
