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