#include <bits/stdc++.h>
using namespace std;
inline long long fpow(long long n, long long k, long long p = 998244353) {
  long long r = 1;
  for (; k; k >>= 1) {
    if (k & 1) r = r * n % p;
    n = n * n % p;
  }
  return r;
}
inline long long inv(long long a, long long p = 998244353) {
  return fpow(a, p - 2, p);
}
inline long long addmod(long long a, long long val, long long p = 998244353) {
  {
    if ((a = (a + val)) >= p) a -= p;
  }
  return a;
}
inline long long submod(long long a, long long val, long long p = 998244353) {
  {
    if ((a = (a - val)) < 0) a += p;
  }
  return a;
}
inline long long mult(long long a, long long b, long long p = 998244353) {
  return (long long)a * b % p;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long t;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    long long ans = 0;
    vector<long long> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a.begin(), a.end());
    vector<long long> pref(n + 1, 0);
    for (int i = 1; i <= n; i++) pref[i] = a[i - 1] + pref[i - 1];
    for (int i = -1; i < k - 1; i++) {
      long long tmp = 0;
      long long money = p;
      if (pref[i + 1] <= p) {
        tmp = i + 1;
        ans = max(ans, tmp);
        money -= pref[i + 1];
      } else
        break;
      int j = i;
      long long last = i;
      while (j + k < n && money > 0) {
        if (money < a[j + k]) break;
        tmp += k;
        money -= a[j + k];
        last = j + k;
        j += k;
      }
      if (last == n - 1) {
        ans = max(ans, tmp);
      } else {
        long long l = last + 1;
        long long r = min(n - 1, last + k);
        long long l1 = l;
        while (l < r) {
          long long mid = l + (r - l + 1) / 2;
          long long sum = pref[mid + 1] - pref[l1];
          if (sum <= money)
            l = mid;
          else
            r = mid - 1;
        }
        if (pref[l + 1] - pref[l1] <= money) {
          tmp += l - l1 + 1;
        }
        ans = max(ans, tmp);
      }
    }
    cout << ans << "\n";
  }
}
