#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
const int INF = 0x3f3f3f3f;
int dp[maxn], a[maxn];
void rua() {
  int n, p, k, ans = 0;
  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 = 0; i <= n; i++) dp[i] = INF;
  dp[0] = 0;
  for (int i = 1; i <= n; i++) {
    dp[i] = min(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;
  }
  printf("%d\n", ans);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) rua();
  return 0;
}
