#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 6;
int good[maxn], dp[maxn];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    for (int i = 1; i <= n; ++i) {
      cin >> good[i];
    }
    sort(good + 1, good + n + 1);
    dp[0] = 0;
    for (int i = 1; i <= n; ++i) {
      dp[i] = dp[i - 1] + good[i];
      if (i >= k) {
        dp[i] = min(dp[i], dp[i - k] + good[i]);
      }
    }
    for (int i = n; i >= 0; --i) {
      if (p >= dp[i]) {
        cout << i << '\n';
        break;
      }
    }
  }
  return 0;
}
