#include <bits/stdc++.h>
bool compare(int a, int b) { return a < b; }
void swapped(char s1, char s2) {
  char temp = s1;
  s1 = s2;
  s2 = temp;
}
long long int modd(long long int n, long long int k) {
  return (k + (n % k)) % k;
}
int gcd(int a, int b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
using namespace std;
void solve() {
  int n, p, k;
  cin >> n >> p >> k;
  int pref = 0;
  int ans = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < k; i++) {
    int sum = pref;
    if (sum > p) break;
    int cnt = i;
    for (int j = i + k - 1; j < n; j += k) {
      if (sum + a[j] <= p) {
        cnt += k;
        sum += a[j];
      } else {
        break;
      }
    }
    pref += a[i];
    ans = max(ans, cnt);
  }
  cout << ans << "\n";
}
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
