#include <bits/stdc++.h>
using namespace std;
template <class T>
void _Print(T &&x) {
  cerr << x << ")"
       << "\n";
}
template <class T, class... S>
void _Print(T &&x, S &&...y) {
  cerr << x << ", ";
  _Print(y...);
}
template <class T>
class compare {
 public:
  bool operator()(pair<T, T> &x, pair<T, T> &y) {
    if (x.first == y.first) {
      return x.second > y.second;
    }
    return x.first > y.first;
  }
};
template <class T>
ostream &operator<<(ostream &os, const pair<T, T> &a) {
  return os << a.first << " " << a.second;
}
long long power(long long a, int b) {
  long long po = 1;
  while (b) {
    if (b & 1) po *= a;
    a *= a;
    b >>= 1;
  }
  return po;
}
template <class T>
pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first + b.first, a.second + b.second};
}
template <class T>
pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first - b.first, a.second - b.second};
}
template <class T>
pair<T, T> operator*(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first * b.first, a.second * b.second};
}
template <class T>
pair<T, T> operator%(const pair<T, T> &a, const pair<T, T> &b) {
  return {a.first % b.first, a.second % b.second};
}
template <class T, class U>
pair<T, T> operator+(const pair<T, T> &a, const U &b) {
  return {a.first + b, a.second + b};
}
template <class T, class U>
pair<T, T> operator*(const pair<T, T> &a, const U &b) {
  return {a.first * b, a.second * b};
}
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, p, k;
    cin >> n >> p >> k;
    int ara[n + 1];
    for (int i = 1; i <= n; i++) cin >> ara[i];
    sort(ara + 1, ara + n + 1);
    long long sum[n + 1];
    memset(sum, 0, sizeof sum);
    for (int i = 1; i <= n; i++) sum[i] += sum[i - 1] + ara[i];
    int ans = 0;
    for (int i = 0; i < k; i++) {
      int tempp = p;
      if (tempp >= sum[i]) {
        tempp -= sum[i];
        ans = max(ans, i);
        int j = i + 1;
        while (j <= n) {
          if (j + k - 1 <= n and ara[j + k - 1] <= tempp) {
            ans = max(ans, j + k - 1);
            tempp -= ara[j + k - 1];
            j += k;
          } else {
            break;
          }
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
