#include <bits/stdc++.h>
using namespace std;
int main() {
  long int tc;
  cin >> tc;
  while (tc--) {
    long long int n, p, k;
    cin >> n >> p >> k;
    long long int a[n], i, j;
    for (i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    long long int pref = 0, count = 0, ans = 0, sum = 0, temp = 0;
    for (i = 0; i < k; i++) {
      temp = pref;
      count = i;
      if (temp > p) break;
      for (j = i + k - 1; j < n; j += k) {
        if (temp + a[j] <= p) {
          count += k;
          temp = temp + a[j];
        } else
          break;
      }
      pref += a[i];
      ans = max(count, ans);
    }
    cout << ans << endl;
  }
}
