#include <bits/stdc++.h>
using namespace std;
inline int read() {
  register int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
const int N = 2e5 + 10;
int n, p, k, a[N], dp[N], ans;
signed main() {
  int T = read();
  while (T--) {
    for (int i = 1; i <= n; i++) dp[i] = 1e9;
    n = read(), p = read(), k = read(), ans = 0;
    for (int i = 1; i <= n; i++) a[i] = read();
    sort(a + 1, a + n + 1);
    for (int i = 1; i <= n; i++)
      if (i < k)
        dp[i] = dp[i - 1] + a[i];
      else
        dp[i] = dp[i - k] + a[i];
    for (int i = 1; i <= n; i++)
      if (dp[i] <= p) ans = i;
    cout << ans << endl;
  }
}
