#include <bits/stdc++.h>
using namespace std;
int di[] = {0, 1, 0, -1};
int dj[] = {1, 0, -1, 0};
const int N = 2e5 + 1, M = 2 * N, SEGN = 2 << (32 - __builtin_clz(N));
const long double PI = 3.141592653589793238463;
int n, a[N], k, p;
long long solve() {
  long long ans = 0, ans2 = 0, sum = 0, sum2 = 0;
  for (int i = 0; i < k; i++) {
    ans2 = i;
    sum = sum2;
    if (sum > p) break;
    for (int j = i + k - 1; j < n; j += k) {
      if (sum + a[j] <= p) {
        ans2 += k;
        sum += a[j];
      } else {
        break;
      }
    }
    sum2 += a[i];
    ans = max(ans, ans2);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int q;
  cin >> q;
  while (q--) {
    cin >> n >> p >> k;
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a, a + n);
    cout << solve() << "\n";
  }
}
