#include <bits/stdc++.h>
#pragma warning(disable : 4996)
using namespace std;
class segTree {
 public:
  vector<int> t;
  int n;
  segTree(int n) {
    t.resize(n * 4 + 5, 0);
    this->n = n;
  }
  void build() {
    for (int i = n - 1; i > 0; --i) t[i] = t[i << 1] + t[i << 1 | 1];
  }
  void update(int p, int value) {
    for (t[p += n] = value; p > 1; p >>= 1) t[p >> 1] = max(t[p], t[p ^ 1]);
  }
  int query(int l, int r) {
    int ret = 0;
    r++;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) ret = max(ret, t[l++]);
      if (r & 1) ret = max(ret, t[--r]);
    }
    return ret;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    vector<long long> a(n);
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    vector<long long> dp(n + 1, 10000000000);
    dp[0] = 0;
    for (long long i = 1; i <= n; i++) {
      dp[i] = min(dp[i], dp[i - 1] + a[i - 1]);
      if (i + k - 1 <= n)
        dp[i + k - 1] = min(dp[i + k - 1], dp[i - 1] + a[i + k - 2]);
    }
    long long ret = 0;
    for (long long i = 1; i <= n; i++) {
      if (dp[i] <= p) ret = max(ret, i);
    }
    cout << ret << "\n";
  }
  return 0;
}
