#include <bits/stdc++.h>
using namespace std;
const long long MAX = 2e5 + 5;
const long long modu = 1e9 + 7;
const long long inf = 1e17;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long t;
  t = 1;
  cin >> t;
  while (t--) {
    long long n, p, k;
    cin >> n >> p >> k;
    long long a[n + 1];
    a[0] = 0;
    for (long long i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    long long dp[n + 1];
    dp[0] = 0;
    for (long long i = 1; i < k; i++) dp[i] = dp[i - 1] + a[i];
    for (long long i = k; i <= n; i++) {
      dp[i] = dp[i - k] + a[i];
    }
    long long ans = 0;
    for (long long i = n; i > 0; i--) {
      if (dp[i] <= p) {
        ans = i;
        break;
      }
    }
    cout << ans << "\n";
  }
}
