#include <bits/stdc++.h>
using namespace std;
long long int mod = 1000000007;
namespace Rahul {
bool isPrime(long long int n) {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 == 0 || n % 3 == 0) return false;
  for (long long int i = 5; i * i <= n; i = i + 6)
    if (n % i == 0 || n % (i + 2) == 0) return false;
  return true;
}
long long int pow(long long int x, long long int n, long long int mod) {
  if (n == 0) return 1;
  long long int res = pow(x, n / 2, mod);
  res = (res * res) % mod;
  if (n % 2) res = (res * x) % mod;
  return res;
}
long long int _gcd(long long int a, long long int b) {
  if (a == 0)
    return b;
  else
    return _gcd(b % a, a);
}
};  // namespace Rahul
using namespace Rahul;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int tc;
  cin >> tc;
  while (tc--) {
    long long int n, p, k;
    cin >> n >> p >> k;
    long long int a[n + 2];
    for (long long int i = 0; i < n; i++) cin >> a[i + 1];
    long long int dp[n + 2];
    memset(dp, 0, sizeof(dp));
    sort(a + 1, a + n + 1);
    dp[0] = 0;
    for (long long int i = 1; i <= n; i++) {
      if (i >= k)
        dp[i] = a[i] + dp[i - k];
      else
        dp[i] = a[i] + dp[i - 1];
    }
    long long int res = 0;
    for (long long int i = 1; i <= n; i++) {
      if (dp[i] <= p) res = i;
    }
    cout << res << endl;
  }
}
