#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> v1(n + 1, 1);
    for (int i = 1; i <= n; i++) {
      cin >> v1[i];
    }
    sort(v1.begin(), v1.end());
    vector<int> pre(n + 1, 0), v2(k, 0);
    int ans = 0;
    for (int i = 1; i <= n; i++) {
      pre[i] = v1[i] + pre[i - 1];
      if (i >= k) v2[i % k] += v1[i];
      int need = v2[i % k] + pre[i % k];
      if (need <= p) ans = i;
    }
    cout << ans << endl;
  }
}
