#include <bits/stdc++.h>
using namespace std;
const int N = 2 * 1e5 + 10;
const long long inf = 1e18 + 100;
const long long mod = 1e9 + 7;
void solve() {
  long long n, p, k;
  cin >> n >> p >> k;
  vector<long long> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr.begin(), arr.end());
  long long ans = 0;
  long long pp = 0;
  for (int i = 0; i < k; i++) {
    long long sum = pp;
    if (pp > p) {
      break;
    }
    long long cnt = i;
    for (int j = i + k - 1; j < n; j += k) {
      if (sum + arr[j] <= p) {
        cnt += k;
        sum += arr[j];
      } else {
        break;
      }
    }
    pp += arr[i];
    ans = max(ans, cnt);
  }
  cout << ans << "\n";
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
