#include <bits/stdc++.h>
using namespace std;
long long dp[200100];
int main() {
  int q;
  cin >> q;
  while (q--) {
    int n, p, k;
    cin >> n >> p >> k;
    memset(dp, 0, sizeof(dp));
    vector<int> v(n);
    for (long long i = 0; i < n; i++) cin >> v[i];
    sort((v).begin(), (v).end());
    dp[0] = v[0];
    for (int i = 0; i < n; i++) {
      dp[i] = dp[i - 1] + v[i];
      if (i >= k - 1) {
        dp[i] = min(dp[i], ((i - k >= 0) ? dp[i - k] + v[i] : v[i]));
      }
    }
    int flag = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (dp[i] <= p) {
        cout << i + 1 << '\n';
        flag = 1;
        break;
      }
    }
    if (!flag) cout << 0 << '\n';
  }
}
