#include <bits/stdc++.h>
const int N = 2e5 + 7;
using namespace std;
int t, n, p, k, a[N], dp[N];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%d%d%d", &n, &p, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; ++i) {
      if (i >= k)
        dp[i] = min(dp[i - 1], dp[i - k]) + a[i];
      else
        dp[i] = dp[i - 1] + a[i];
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
      if (p >= dp[i]) ans = max(ans, i);
    cout << ans << endl;
  }
  return 0;
}
