#include <bits/stdc++.h>
using namespace std;
namespace {
int n, p, k;
const int maxn = 200001;
int a[maxn];
int dp[maxn];
}  // namespace
int main() {
  int ntc;
  scanf("%d\n", &ntc);
  for (int tc = 0; tc < ntc; ++tc) {
    scanf("%d%d%d\n", &n, &p, &k);
    for (int i = 0; i < n; ++i) {
      scanf("%d", &a[i]);
    }
    sort(a, a + n);
    int best = 0;
    for (int i = 0; i < n; ++i) {
      dp[i] = a[i];
      if (i > 0 && i != k - 1) dp[i] += dp[i - 1];
      if (i >= k) dp[i] = min(dp[i], dp[i - k] + a[i]);
    }
    for (int i = 0; i < n; ++i) {
      if (dp[i] <= p) {
        best = i + 1;
      }
    }
    printf("%d\n", best);
  }
  return 0;
}
