#include <bits/stdc++.h>
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "[ ";
  for (auto v : V) os << v << " ";
  return os << "]";
}
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
const long long int mod = 1e9 + 7;
const long long int maxn = 3 * 1000001;
const long long int BLOCK = 555;
long long int power(long long int x, long long int n) {
  long long int res = 1;
  for (; n > 0; n >>= 1) {
    if (n & 1) res = (res * x) % mod;
    x = (x * x) % mod;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, p, k;
    cin >> n >> p >> k;
    long long int a[n + 1];
    for (long long int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    long long int pre[n + 1];
    pre[0] = 0;
    for (long long int i = 1; i < k; i++) {
      pre[i] = pre[i - 1] + a[i];
    }
    for (long long int i = k; i <= n; i++) {
      pre[i] = pre[i - k] + a[i];
    }
    long long int ans = 0;
    for (long long int i = 0; i <= n; i++) {
      if (pre[i] <= p) {
        ans = i;
      }
    }
    cout << ans << endl;
  }
}
