#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
bool comp(pair<long long, long long> p, pair<long long, long long> q) {
  if (p.first != q.first)
    return p.first < q.first;
  else
    return p.second > q.second;
}
long long power(long long a, long long n) {
  long long res = 1;
  while (n) {
    if (n % 2) res = (res % mod * a % mod) % mod;
    a = (a % mod * a % mod) % mod;
    n /= 2;
  }
  return res % mod;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t, n, p, k;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    long long m = p;
    long long a[n], ma = 0;
    for (long long i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    if (a[0] > p) {
      cout << 0 << "\n";
      continue;
    }
    long long s = 0, ans = 0;
    for (long long i = 0; i < k; i++) {
      long long md = 0;
      ma = i;
      for (long long j = i + k - 1; j < n; j += k) {
        if (a[j] <= p - (s + md)) {
          ma += k;
          md += a[j];
        } else
          break;
      }
      ans = max(ma, ans);
      s += a[i];
      if (s > p) break;
    }
    cout << ans << "\n";
  }
}
