#include <bits/stdc++.h>
using namespace std;
long long a[200010], sum[200010];
int main() {
  int T;
  cin >> T;
  while (T--) {
    int n, p, k;
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    sort(a + 1, a + 1 + n);
    long long ans = 0;
    sum[0] = 0;
    sum[1] = a[1];
    for (int i = 2; i <= k; i++) sum[i] = sum[i - 1] + a[i];
    for (int i = k; i <= n; i++) sum[i] = sum[i - k] + a[i];
    for (int i = 0; i <= n; i++)
      if (p >= sum[i]) ans = i;
    cout << ans << endl;
  }
  return 0;
}
