#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int a[maxn], dp[maxn];
int main() {
  int t, n, p, k, ans;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d %d", &n, &p, &k);
    for (int i = 1; i <= n; ++i) {
      scanf("%d", &a[i]);
    }
    sort(a + 1, a + n + 1);
    memset(dp, -1, sizeof(dp));
    dp[0] = p, ans = 0;
    for (int i = 1; i < k; ++i) {
      dp[i] = dp[i - 1] - a[i];
    }
    for (int i = k; i <= n; ++i) {
      if (i - k >= 0) {
        dp[i] = max(dp[i], dp[i - k] - a[i]);
      }
      dp[i] = max(dp[i], dp[i - 1] - a[i]);
    }
    for (int i = 1; i <= n; ++i) {
      if (dp[i] >= 0) {
        ans = i;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
