#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long power(long long first, long long n) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * first) % 1000000007;
    first = (first * first) % 1000000007;
    n = n / 2;
  }
  return result % 1000000007;
}
class dsu {
  vector<long long> p, rank;
  vector<long long> set_count;

 public:
  dsu(long long n) {
    p.assign(n, 0);
    rank.assign(n, 0);
    set_count.assign(n, 1);
    for (long long i = 0; i < n; i++) p[i] = i;
  }
  long long findSet(long long first) {
    return (p[first] == first) ? first : (p[first] = findSet(p[first]));
  }
  bool isSame(long long i, long long j) { return (findSet(i) == findSet(j)); }
  void unionSet(long long i, long long j) {
    if (!isSame(i, j)) {
      long long first = findSet(i), second = findSet(j);
      if (rank[first] > rank[second]) {
        p[second] = first;
        set_count[first] += set_count[second];
      } else {
        p[first] = second;
        set_count[second] += set_count[first];
        if (rank[first] == rank[second]) rank[second]++;
      }
    }
  }
  long long count(long long first) { return set_count[findSet(first)]; }
};
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    vector<long long> v(n);
    for (long long &first : v) cin >> first;
    sort((v).begin(), (v).end());
    long long dp[n + 1];
    memset(dp, 0, sizeof(dp));
    for (long long i = 0; i < k - 1; i++) {
      dp[i + 1] = v[i] + dp[i];
    }
    for (long long i = k - 1; i < n; i++) {
      dp[i + 1] = v[i] + dp[i - k + 1];
    }
    long long ans = 0;
    for (long long i = n; i >= 0; i--) {
      if (dp[i] <= p) {
        ans = i;
        break;
      }
    }
    cout << ans << '\n';
  }
}
