#include <bits/stdc++.h>
using namespace std;
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    int N, P, K;
    scanf("%d", &N), scanf("%d", &P), scanf("%d", &K);
    vector<int> arr(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &arr[i]);
    }
    sort((arr).begin(), (arr).end());
    int prefix[N + 5];
    prefix[0] = 0;
    for (int i = 1; i <= N; ++i) {
      prefix[i] = prefix[i - 1] + arr[i - 1];
    }
    int MAX = 0;
    for (int i = 0; i < K; ++i) {
      int cnt = lower_bound((prefix), (prefix) + (i + 1), P + 1) - prefix;
      --cnt;
      int P_tmp = P - prefix[cnt];
      int j = cnt - 1;
      j += K;
      for (; j < N && P_tmp > 0; j += K) {
        if (P_tmp >= arr[j]) {
          P_tmp -= arr[j];
          cnt += K;
        } else {
          break;
        }
      }
      MAX = max(MAX, cnt);
    }
    printf("%d\n", MAX);
  }
  return 0;
}
