#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    vector<int> v(n);
    for (int i = 0; i < n; i++) cin >> v[i];
    sort(v.begin(), v.end());
    vector<int> dp(n + 1);
    dp[0] = 0;
    int c = 1;
    int res = 0;
    while (c <= n) {
      dp[c] = v[c - 1] + (c >= k ? dp[c - k] : dp[c - 1]);
      if (dp[c] <= p) res = c;
      c++;
    }
    cout << res << '\n';
  }
}
