#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
const int N = 1e5 + 9, M = 2e9, OO = 0x3f3f3f3f;
const long long int MOD = 1000000007;
const double PI = acos(-1);
int main() {
  int t;
  scanf("%d", &t);
  ;
  while (t--) {
    long long int n, p, k, arr[2 * N];
    cin >> n >> p >> k;
    for (int i = 0; i < int(n); ++i) cin >> arr[i];
    long long int sum = 0, pref = 0, Max = 0;
    sort(arr, arr + n);
    for (int i = 0; i < int(k); ++i) {
      long long int cnt = i;
      sum = pref;
      if (sum > p) {
        break;
      }
      for (int j = i + k - 1; j < n; j += k) {
        if (sum + arr[j] <= p) {
          sum += arr[j];
          cnt += k;
        } else
          break;
      }
      pref += arr[i];
      Max = max(Max, cnt);
    }
    cout << Max << '\n';
  }
}
