#include <bits/stdc++.h>
using namespace std;
inline int read(int f = 1, int x = 0, char ch = ' ') {
  while (!isdigit(ch = getchar()))
    if (ch == '-') f = -1;
  while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
  return f * x;
}
const int N = 2e5 + 5;
int m, n, T, s[2][N], c[2], f[2], ans, q[N], t[N], type[N], tot;
long long a, b;
int main() {
  m = read();
  while (m--) {
    n = read(), T = read(), a = read(), b = read();
    c[0] = c[1] = f[0] = f[1] = ans = 0;
    for (int i = 1; i <= n; ++i) type[i] = read(), s[0][i] = s[1][i] = 0;
    for (int i = 1; i <= n; ++i) q[i] = t[i] = read();
    sort(q + 1, q + 1 + n), tot = unique(q + 1, q + 1 + n) - q - 1;
    for (int i = 1; i <= n; ++i) {
      int j = lower_bound(q + 1, q + 1 + tot, t[i]) - q;
      ++s[type[i]][j], ++c[type[i]];
    }
    q[tot + 1] = max(q[tot] + 1, T + 1);
    for (int i = 0; i <= tot; ++i) {
      f[0] += s[0][i], f[1] += s[1][i];
      int t = min(q[i + 1] - 1, T), resa = c[0] - f[0], resb = c[1] - f[1];
      ;
      if (f[0] * a + f[1] * b > t) continue;
      int rest = t - (f[0] * a + f[1] * b);
      if (resa * a <= rest)
        ans = max(f[0] + f[1] + resa + min((rest - resa * a) / b, 1ll * resb),
                  1ll * ans);
      else
        ans = max(f[0] + f[1] + rest / a, 1ll * ans);
    }
    printf("%d\n", ans);
  }
  return 0;
}
