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