#include <bits/stdc++.h>
using namespace std;
const long long MAX = 2e5 + 5;
long long arr[MAX];
bool can(long long n, long long p, long long k, long long g) {
  long long r = g % k, q = g / k;
  for (long long i = 1; i < r + 1; i++) p = p - arr[i];
  for (long long i = 1; i < q + 1; i++) {
    if (r + i * k <= n)
      p = p - arr[r + i * k];
    else
      break;
  }
  return (p >= 0);
}
void func() {
  long long n, p, k;
  cin >> n >> p >> k;
  for (long long i = 1; i < n + 1; i++) cin >> arr[i];
  sort(arr + 1, arr + n + 1);
  long long cost = 0, mx = 0;
  for (long long i = 1; i < n / k + 1; i++) {
    cost += arr[i * k];
    if (cost <= p)
      mx = i;
    else
      break;
  }
  long long l = k * mx, r = n, mid, ans = -1;
  while (l <= r) {
    mid = (l + r) / 2;
    if (can(n, p, k, mid) == 1)
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  cout << ans << endl;
}
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long q = 1;
  cin >> q;
  while (q) {
    func();
    q--;
  }
  return 0;
}
