#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
int n, p, k;
int a[maxn];
int dp[maxn];
int cnt[maxn];
int main(int arc, char const *argv[]) {
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + 1 + n);
    for (int i = 1; i < k; i++) {
      dp[i] = dp[i - 1] + a[i];
      cnt[i] = cnt[i - 1] + 1;
    }
    for (int i = k; i <= n; i++) {
      dp[i] = dp[i - k] + a[i];
      cnt[i] = cnt[i - k] + k;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      if (dp[i] <= p) {
        ans = max(ans, cnt[i]);
      }
    }
    cout << ans << endl;
  }
}
