#include <bits/stdc++.h>
using namespace std;
int a[200000];
int s[200000];
int n, p, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> p >> k;
    for (int i = 0; i < n; ++i) cin >> a[i];
    sort(a, a + n);
    int mx;
    if (a[0] <= p) {
      mx = 1;
    } else {
      cout << "0\n";
      continue;
    }
    s[0] = a[0];
    for (int i = 1; i <= k - 2; ++i) {
      s[i] = s[i - 1] + a[i];
      if (s[i] <= p) mx = i + 1;
    }
    for (int i = 0; i <= k - 1; i++) {
      int z = 0;
      int y = 0;
      for (int j = i + k - 1; j < n; j += k) {
        if (z + a[j] > p) break;
        z += a[j];
        y++;
      }
      if (i > 0) {
        int add = upper_bound(s, s + i, p - z) - s - 1;
        if (add >= 0 && p - z >= s[add]) mx = max(mx, y * k + add + 1);
      } else
        mx = max(mx, y * k);
    }
    cout << mx << '\n';
  }
  return 0;
}
