#include <bits/stdc++.h>
using namespace std;
const double PI = 2 * acos(0.0);
const int Mod = 1E9 + 7;
inline void yes() { std::cout << "Yes\n"; }
inline void no() { std::cout << "No\n"; }
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, p, k, ans = 0;
    cin >> n >> p >> k;
    long long int a[n + 1];
    for (long long int i = (1); i < (n + 1); i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    long long int low = 1, high = n, mid;
    long long int i;
    while (low <= high) {
      mid = (low + high) / 2;
      long long int cnt = 0;
      long long int flag = 1;
      long long int sum = 0;
      for (i = mid; i >= k; i = i - k) {
        sum = sum + a[i];
        if (sum > p) {
          flag = 0;
          break;
        }
        cnt++;
      }
      cnt = cnt * k;
      for (long long int j = 1; j < i + 1; j++) {
        sum = sum + a[j];
        if (sum <= p)
          cnt++;
        else
          break;
      }
      if (flag) {
        low = mid + 1;
        ans = max(ans, cnt);
      } else {
        high = mid - 1;
      }
    }
    cout << ans << '\n';
  }
}
