#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T>
T gcd(T a, T b) {
  return b ? gcd(b, a % b) : a;
}
const long long mod = 1000000007;
const long long LINF = 1LL << 62;
const int INF = 1 << 30;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long n, p, k;
    cin >> n >> p >> k;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    long long ans = 0;
    long long s = 0;
    for (int i = 0; i <= k; i++) {
      long long sum = s;
      if (sum > p) break;
      long long tmp = i;
      int id = i + k - 1;
      while (id < n) {
        if (sum + a[id] > p) break;
        sum += a[id];
        id += k;
        tmp += k;
      }
      ans = max(ans, tmp);
      s += a[i];
    }
    cout << ans << endl;
  }
  return 0;
}
