#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 210000;
const int M = 1e9 + 7;
long long a[maxn];
long long b[maxn];
int main() {
  int n, k, t;
  long long p;
  cin >> t;
  while (t--) {
    cin >> n >> p >> k;
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
    }
    sort(a + 1, a + n + 1);
    long long res = 0, ans = 0;
    for (int i = 1; i < k; i++) {
      b[i] = b[i - 1] + a[i];
      if (b[i] <= p) ans = i;
    }
    for (int i = 0; i < k; i++) {
      for (int j = i + k; j <= n; j += k) {
        b[i] += a[j];
        if (b[i] <= p && j > ans) ans = j;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
