#include <bits/stdc++.h>
using namespace std;
int n;
int p;
int k;
int a[200000];
void read_data(void) {
  cin >> n;
  cin >> p;
  cin >> k;
  for (int i = 0; i < n; i++) cin >> a[i];
}
bool is_possible(int l) {
  if (l > n) return false;
  int r = l;
  int p_copy = p;
  for (;;) {
    if ((r - k) >= 0) {
      p_copy -= a[r - 1];
      if (p_copy < 0) return false;
      r -= k;
    } else
      break;
  }
  for (int i = 0; i < r; i++) {
    p_copy -= a[i];
    if (p < 0) return false;
  }
  if (p_copy < 0) return false;
  return true;
}
int nearest(int x, int j) {
  int bf = x - (x % j);
  int af = bf + j;
  if (abs(x - bf) < abs(x - af)) return bf;
  return af;
}
int dicho(int j, int dd, int ff) {
  int d = dd;
  int f = ff;
  int m = (d + f) / 2;
  while (f >= d) {
    m = (d + f) / 2;
    m = nearest(m, j);
    if (is_possible(m)) {
      if (((m + j) > n) || !is_possible(m + j))
        return m;
      else
        d = m + j;
    } else {
      if (m == dd) return dd - 1;
      f = m - j;
    }
  }
  m = (d + f) / 2;
  return m;
}
void solve_case(void) {
  sort(a, a + n);
  int d = 0;
  int f = n - (n % k);
  if (f < n) f += k;
  int m_k = dicho(k, d, f);
  int m = dicho(1, m_k + 1, m_k + k);
  cout << m << endl;
}
void solve(void) {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    read_data();
    solve_case();
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  return 0;
}
