#include <bits/stdc++.h>
using namespace std;
int dp[200005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    int a[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    sort(a, a + n);
    int dp[n];
    memset(dp, INT_MAX, sizeof(dp));
    dp[0] = a[0];
    for (int i = 1; i < n; i++) {
      dp[i] = a[i] + dp[i - 1];
      if (i >= k - 1) {
        dp[i] = min(a[i] + ((i >= k) ? dp[i - k] : 0), dp[i]);
      }
    }
    int ans = -1;
    for (int i = 0; i < n; i++) {
      if (p >= dp[i]) {
        ans = i;
      }
    }
    cout << ans + 1 << "\n";
  }
  return 0;
}
