#include <bits/stdc++.h>
using namespace std;
vector<long long> prices;
vector<long long> pricessum;
long long n, p, k;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  for (int tc = 0; tc < t; tc++) {
    cin >> n >> p >> k;
    prices = vector<long long>(n);
    pricessum = vector<long long>(n, 0);
    for (int i = 0; i < n; i++) {
      cin >> prices[i];
    }
    sort(prices.begin(), prices.end());
    pricessum[0] = prices[0];
    for (int i = 1; i < n; i++) {
      pricessum[i] = pricessum[i - 1] + prices[i];
    }
    long long maxnum = 0;
    for (int i = -1; i < k - 1; i++) {
      long long cost = 0;
      long long num = i + 1;
      if (i != -1) {
        cost = pricessum[i];
      }
      if (cost > p) {
        continue;
      }
      int j = i;
      j += k;
      while (j < n) {
        if (cost + prices[j] > p) {
          break;
        }
        num += k;
        cost += prices[j];
        j += k;
      }
      maxnum = max(maxnum, num);
    }
    cout << maxnum << endl;
  }
}
