#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MOD = 1e9 + 7;
int arr[201010];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, p, k;
    scanf("%d %d %d", &n, &p, &k);
    for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
    sort(arr, arr + n);
    int ans = 0;
    int pref = 0;
    for (int i = 0; i <= k - 1; i++) {
      int cnt = i;
      if (i) pref += arr[i - 1];
      if (pref > p) break;
      int pls = 0;
      for (int j = i - 1 + k; j < n; j += k) {
        pls += arr[j];
        if (pref + pls > p) break;
        cnt += k;
      }
      ans = max(ans, cnt);
    }
    printf("%d\n", ans);
  }
  return 0;
}
