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