#include <bits/stdc++.h>
using namespace std;
int dp[200005];
int a[200005];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, p, k;
    cin >> n >> p >> k;
    dp[0] = 0;
    for (int i = 1; i <= n; i++) cin >> a[i], dp[i] = 0;
    sort(a + 1, a + 1 + n);
    int ans = 0;
    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 << endl;
  }
  return 0;
}
