#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, p, k;
    cin >> n >> p >> k;
    long long int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    if (p < a[0])
      cout << 0;
    else {
      long long int ans1 = 0, pre[n], x, max1 = 0, y;
      x = p;
      y = p;
      vector<long long int> v;
      pre[0] = a[0];
      for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i];
      for (int i = k - 1; (i < n && p > 0); i += k) {
        p -= a[i];
        if (p >= 0) ans1 += k;
      }
      for (int i = 0; i < k - 1; i++) {
        long long int ans2 = 0;
        x = y;
        x -= pre[i];
        if (x >= 0) ans2 = i + 1;
        for (int j = k + i; j < n && x > 0; j += k) {
          x -= a[j];
          if (x >= 0) ans2 += k;
        }
        v.push_back(ans2);
      }
      for (unsigned int i = 0; i < v.size(); i++) {
        if (max1 < v[i]) max1 = v[i];
      }
      cout << max(ans1, max1);
    }
    cout << endl;
  }
  return 0;
}
