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