#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
long long int a[N];
long long int dp[N];
int main() {
  int n;
  cin >> n;
  while (n--) {
    long long int x, p, k;
    cin >> x >> p >> k;
    memset(dp, 0, sizeof(dp));
    for (long long int i = 0; i < x; i++) {
      cin >> a[i];
    }
    sort(a, a + x);
    dp[0] = a[0];
    if (p < a[0]) {
      cout << "0" << endl;
      continue;
    } else {
      for (long long int i = 1; i < x; i++) {
        if ((i - k + 1) >= 0)
          dp[i] = min(dp[i - 1] + a[i], dp[i - k] + a[i]);
        else
          dp[i] = dp[i - 1] + a[i];
      }
    }
    for (long long int i = x - 1; i >= 0; i--) {
      if (dp[i] <= p) {
        cout << i + 1 << endl;
        break;
      }
    }
  }
  return 0;
}
