#include <bits/stdc++.h>
using namespace std;
const int MX = 1000000 + 5;
const long long inf = 1e9;
const long long mod = 1e9 + 7;
int a[MX], k;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m, p, x, y, tc;
  cin >> tc;
  while (tc--) {
    cin >> n >> p >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    sort(a + 1, a + n + 1);
    int rs = 0, pre = 0;
    for (int i = 1; i <= k; i++) {
      int sm = pre;
      if (pre > p) break;
      rs = max(rs, i - 1);
      for (int j = i + k - 1; j <= n; j += k) {
        sm += a[j];
        if (sm > p) break;
        rs = max(rs, j);
      }
      pre += a[i];
    }
    cout << rs << endl;
  }
  return 0;
}
