#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 1e5 + 7;
int tt;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> tt;
  while (tt--) {
    int n, p, k, a[maxn], dp[maxn], ans = 0;
    cin >> n >> p >> k;
    for (int i = 0; i <= n; i++) dp[i] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++) {
      dp[i] = dp[i - 1] + a[i];
      if (i >= k) dp[i] = min(dp[i], dp[i - k] + a[i]);
      if (dp[i] <= p) ans = i;
    }
    cout << ans << '\n';
  }
}
