#include <bits/stdc++.h>
using namespace std;
vector<int> v, pref;
vector<int> prefk[200005];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, p, k;
    scanf("%d%d%d", &n, &p, &k);
    v.resize(n + 1);
    pref.resize(n + 1);
    for (int i = 0; i < k; i++) prefk[i].clear();
    for (int i = 1; i <= n; i++) scanf("%d", &v[i]);
    sort(v.begin(), v.end());
    pref[0] = 0;
    for (int i = 1; i <= n; i++) {
      pref[i] = pref[i - 1] + v[i];
      if (i >= k) {
        if (prefk[i % k].size() == 0)
          prefk[i % k].push_back(v[i]);
        else
          prefk[i % k].push_back(prefk[i % k][prefk[i % k].size() - 1] + v[i]);
      }
    }
    for (int i = 0; i < k; i++) {
      cerr << i << ": ";
      for (auto j : prefk[i]) {
        cerr << j << " ";
      }
      cerr << endl;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      int sisa = i % k, tmp = 0;
      if (sisa != 0) tmp = pref[sisa];
      if (i >= k) {
        int bagi = i / k - 1;
        tmp += prefk[sisa][bagi];
      }
      if (tmp <= p) ans = i;
    }
    printf("%d\n", ans);
  }
  return 0;
}
