#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
long long n, p, k;
long long func(long long ind, long long a[], long long dp[]) {
  if (ind >= 0) {
    if (dp[ind] != -1) {
      return dp[ind];
    }
  }
  if (ind < 0) {
    return 0;
  }
  if (ind + 1 >= k) {
    return dp[ind] = a[ind] + func(ind - k, a, dp);
  } else {
    return dp[ind] = a[ind] + func(ind - 1, a, dp);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    long long ind = n;
    long long dp[n + 1];
    memset(dp, -1, sizeof(dp));
    long long a[n];
    for (long long i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    for (long long i = 0; i < n; i++) {
      if (a[i] > p) {
        ind = i;
        break;
      }
    }
    long long max1 = 0;
    for (long long i = 0; i < ind; i++) {
      long long cost = func(i, a, dp);
      if (cost <= p) {
        max1 = i + 1;
      }
    }
    cout << max1 << "\n";
  }
}
