#include <bits/stdc++.h>
using namespace std;
const int m = 200005;
int a[m];
int main() {
  int t, n, p, k, sum, prev, cnt, ans;
  scanf("%d", &t);
  while (t--) {
    scanf("%d %d %d", &n, &p, &k);
    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
    }
    sort(a, a + n);
    prev = 0;
    ans = 0;
    for (int i = 0; i < k; i++) {
      sum = prev;
      if (sum > p) {
        break;
      }
      cnt = i;
      for (int j = i + k - 1; j < n; j += k) {
        if (sum + a[j] <= p) {
          cnt += k;
          sum += a[j];
        } else {
          break;
        }
      }
      prev += a[i];
      ans = max(ans, cnt);
    }
    printf("%d\n", ans);
  }
  return 0;
}
