#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  int64_t t;
  cin >> t;
  while (t--) {
    int64_t n, p, k;
    cin >> n >> p >> k;
    int64_t a[n], i, j;
    for (i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    int64_t ans = 0, cnt, sum, pre = 0;
    for (i = 0; i < k; i++) {
      sum = pre;
      cnt = i;
      if (sum > p) break;
      for (j = i + k - 1; j < n; j += k) {
        if (sum + a[j] <= p) {
          cnt += k;
          sum += a[j];
        } else
          break;
      }
      pre += a[i];
      ans = max(ans, cnt);
    }
    cout << ans << endl;
  }
}
