#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 6;
const int inf = 0x3f3f3f3f;
long long a[M], sum[M];
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    int n, p, k;
    scanf("%d%d%d", &n, &p, &k);
    for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]);
    sort(a + 1, a + 1 + n);
    for (int i = 1; i <= n; i++) sum[i] = sum[i - 1] + a[i];
    int ans = 0;
    for (int i = 0; i < k; i++) {
      long long now = sum[i];
      int num = i;
      if (now > p) {
        num--;
        ans = max(ans, num);
        break;
      }
      for (int j = i + k; j <= n; j += k) {
        now += a[j];
        num += k;
        if (now > p) {
          num -= k;
          break;
        }
      }
      ans = max(ans, num);
    }
    printf("%d\n", ans);
  }
  return 0;
}
