#include <bits/stdc++.h>
using namespace std;
const int maxn = 200000 + 10;
int a[maxn], T, p, k, n, dp[maxn];
int main() {
  cin >> T;
  while (T--) {
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + 1 + n);
    memset(dp, 0x7f7f7f7f, sizeof(dp));
    dp[0] = 0;
    for (int i = 1; i <= n; i++) {
      if (i >= k)
        dp[i] = min(dp[i], dp[i - k] + a[i]);
      else
        dp[i] = min(dp[i], dp[i - 1] + a[i]);
    }
    int ans = 0;
    for (int i = n; i >= 1; i--) {
      if (dp[i] <= p) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
