#include <bits/stdc++.h>
using namespace std;
char sl = '\n';
int pr[2];
long long a, b;
struct tx {
  int time;
  int easy;
  int dff;
  tx(int _a, int _b, int _c) {
    time = _a;
    easy = _b;
    dff = _c;
  }
};
long long god(long long easy, long long dff, long long time, long long must) {
  long long ans(0);
  time -= must;
  if (time > 0) {
    int x = 0;
    x = min(easy, time / a);
    ans = x;
    time -= (x * a);
    ans += min((time / b), dff);
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int q;
  cin >> q;
  while (q--) {
    long long n, T;
    cin >> n >> T >> a >> b;
    vector<bool> nb(n);
    for (int i = 0; i < n; ++i) {
      bool x;
      cin >> x;
      nb[i] = x;
      pr[x]++;
    }
    map<int, pair<int, int> > vi;
    vector<tx> ff;
    for (int i = 0; i < n; ++i) {
      int t;
      cin >> t;
      if (nb[i])
        vi[t].first++;
      else
        vi[t].second++;
    }
    long long ans(0);
    for (auto [i, j] : vi) ff.emplace_back(i, j.second, j.first);
    pair<long long, long long> doit = {0LL, 0LL};
    for (tx i : ff) {
      long long x = doit.first * a + doit.second * b;
      long long z = god(pr[0] - doit.first, pr[1] - doit.second, i.time - 1, x);
      x = x > (i.time - 1) ? 0LL : doit.first + doit.second + z;
      long long y = i.easy * a + i.dff * b + doit.first * a + doit.second * b;
      if (y > i.time)
        y = 0;
      else
        y = i.easy + i.dff + doit.first + doit.second +
            god(pr[0] - doit.first - i.easy, pr[1] - doit.second - i.dff,
                i.time, y);
      ans = max(ans, max(x, y));
      doit.first += i.easy;
      doit.second += i.dff;
    }
    long long tmp1 = doit.first * a + doit.second * b;
    if (tmp1 > T)
      tmp1 = 0;
    else
      tmp1 = n;
    cout << max(ans, tmp1) << sl;
    pr[0] = pr[1] = 0;
  }
  return 0;
}
