#include <bits/stdc++.h>
using namespace std;
const long long MAX = 5e5 + 3;
const long long mod = 1e9 + 7;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    vector<long long> v(n);
    for (long long i = 0; i < n; i++) {
      cin >> v[i];
    }
    sort((v).begin(), (v).end());
    long long dp[MAX] = {0};
    long long val = 0;
    dp[0] = 0;
    for (long long i = 1; i < n + 1; i++) {
      if (i < k) {
        dp[i] = v[i - 1] + dp[i - 1];
      } else {
        dp[i] = v[i - 1] + dp[i - k];
      }
      if (dp[i] <= p) {
        val = i;
      }
    }
    cout << val << "\n";
    ;
  }
  return 0;
}
