#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, t, a, b;
    cin >> n >> t >> a >> b;
    long long ar1[n], ar2[n];
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      cin >> ar1[i];
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      cin >> ar2[i];
    long long tota = 0, totb = 0;
    map<long long, long long> m1, m2a, m2b;
    for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n));
         i += 1 - 2 * ((0) > (n)))
      if (ar1[i] == 0)
        tota++, m1[ar2[i]] += a, m2a[ar2[i]]++;
      else
        totb++, m1[ar2[i]] += b, m2b[ar2[i]]++;
    vector<long long> mar2;
    auto it = m1.begin();
    long long tmptime = (it->first) - 1;
    it++;
    while (it != m1.end()) {
      mar2.push_back((it->first) - 1);
      it++;
    }
    mar2.push_back(t);
    long long ansa = 0, ansb = 0, fans = 0;
    long long curr = 0;
    if (tmptime > 0) {
      long long ttya = tmptime / a;
      fans += min(ttya, tota);
      tmptime -= a * min(ttya, tota);
      long long ttyb = tmptime / b;
      fans += min(ttyb, totb);
    }
    long long tmp = 0;
    for (auto it : m1) {
      curr += it.second;
      ansa += m2a[it.first];
      ansb += m2b[it.first];
      if (curr <= mar2[tmp]) {
        long long tans = ansa + ansb;
        long long time = mar2[tmp] - curr;
        long long ta = tota - ansa;
        long long tb = totb - ansb;
        long long tya = time / a;
        tans += min(tya, ta);
        time -= a * min(tya, ta);
        long long tyb = time / b;
        tans += min(tyb, tb);
        time -= b * min(tyb, tb);
        fans = max(fans, tans);
      }
      tmp++;
    }
    cout << fans << '\n';
  }
  return 0;
}
