#include <bits/stdc++.h>
using namespace std;
long long cc = 10000000089;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, p, k, i;
    cin >> n >> p >> k;
    vector<long long> a(n), v(n), sum(n);
    for (i = 0; i < n; i++) v[i] = 0;
    for (i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a.begin(), a.end());
    for (i = 0; i < n; i++) {
      if (!i)
        sum[i] = a[i];
      else
        sum[i] = sum[i - 1] + a[i];
    }
    long long ans = 0;
    ans = lower_bound(sum.begin(), sum.end(), p + 1) - sum.begin();
    for (i = k - 1; i < n; i++) {
      if (i == k - 1)
        v[i] = a[i];
      else
        v[i] = v[i - k] + a[i];
      if (v[i] <= p) {
        long long c, cc;
        c = p - v[i];
        cc = lower_bound(sum.begin(), sum.end(), c + 1) - sum.begin();
        cc = min((i + 1) % k, cc);
        ans = max(ans, ((i + 1) / k) * k + cc);
      }
    }
    cout << ans << endl;
  }
}
