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