#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int t;
long long n, p, k;
long long a[N], sum[N];
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld%lld", &n, &p, &k);
    for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    sort(a + 1, a + 1 + n);
    int num = -1;
    for (int i = 1; i < k; i++) {
      sum[i] = sum[i - 1] + a[i];
      if (sum[i] > p && num == -1) num = i - 1;
    }
    if (a[k] > p) {
      if (num == -1) num = k - 1;
      printf("%d\n", num);
      continue;
    }
    int flag = 0;
    num = k - 1;
    for (int i = k; i <= n; i++) {
      sum[i] = sum[i - k] + a[i];
      if (sum[i] <= p) num = i;
    }
    printf("%d\n", num);
  }
  return 0;
}
