#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 3;
int n, p, k, a[N];
long long cumA[N];
vector<pair<long long, int>> cumK[N];
int getIdx(int st, int idx) {
  int lo = 0, hi = (int)cumK[idx].size() - 1, ret = -1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (cumK[idx][mid].second < st) {
      ret = mid;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  return ret;
}
int bs(int st, int p) {
  int idx = (st + k - 1) % k;
  int me = getIdx(st, idx);
  int lo = me + 1, hi = (int)cumK[idx].size() - 1, ret = st - 1;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    long long need = cumK[idx][mid].first;
    if (me >= 0) need -= cumK[idx][me].first;
    if (need <= p) {
      ret = cumK[idx][mid].second;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  return ret;
}
int solve(int st, int p) {
  if (cumA[st - 1] > p) return 0;
  p -= cumA[st - 1];
  return bs(st, p);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d %d", &n, &p, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    sort(a + 1, a + n + 1);
    for (int i = 0; i < k; ++i) cumK[i].clear();
    for (int i = 1; i <= n; ++i) {
      cumA[i] = cumA[i - 1] + a[i];
      if (cumK[i % k].empty())
        cumK[i % k].emplace_back(a[i], i);
      else
        cumK[i % k].emplace_back(cumK[i % k].back().first + a[i], i);
    }
    int res = 0;
    for (int i = 1; i <= k; ++i) {
      res = max(res, solve(i, p));
    }
    printf("%d\n", res);
  }
  return 0;
}
