#include <bits/stdc++.h>
using namespace std;
const int N = 500005;
int a[N], dp[N];
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    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 = 1; i <= n; i++) {
      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);
  }
  return 0;
}
