#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define PB push_back
#define PF push_front
#define MK make_pair
#define MT make_tuple
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define mod 1000000007

void CutTree(map<int, set<int>> &NB, int &etr) {
  // check if the remaining graph is a line or not
  bool isline = true;
  for (auto nd : NB) {
    if (nd.second.size() > 2)
      isline = false;
  }
  if (isline) {
    etr += NB.size() / 2 - 1;
    NBclear();
  } else {
    // find all leaf
    set<int> Leaf, Cent;
    for (auto nd : NB) {
      if (nd.second.size() == 1)
        Leaf.insert(nd.first);
    }
    // Process the leaves
    for (auto e : Leaf) {
      int child = e, parent = *(NB[child].begin());
      while ((NB[child].size() == 1) && (NB[parent].size() == 2)) {
        etr++;
        NB.erase(child);
        NB[parent].erase(child);
        child = *(NB[parent].begin());
        NB.erase(parent);
        NB[child].erase(parent);
        parent = *(NB[child].begin());
      }
    }
    // Merge the leaves
    for (auto nd : NB) {
      if (nd.second.size() > 1)
        Cent.insert(nd.first);
    }
    for (auto n : Cent) {
      vector<int> CentLeaf;
      for (auto d : NB[n])
        if (NB[d].size() == 1)
          CentLeaf.PB(d);
      for (size_t i = 0; i < CentLeaf.size() / 2; i++) {
        NB[n].erase(CentLeaf[2 * i]);
        NB[n].erase(CentLeaf[2 * i + 1]);
        NB.erase(CentLeaf[2 * i]);
        NB.erase(CentLeaf[2 * i + 1]);
      }
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int N, x, y;
  cin >> N;
  map<int, set<int>> NB;

  REP(i, 1, N - 1) {
    cin >> x >> y;
    NB[x].insert(y);
    NB[y].insert(x);
  }
  if (N & 1)
    cout << -1 << endl;
  else {
    int etr = 0;
    while (!NB.empty())
      CutTree(NB, etr);
    cout << etr << endl;
  }

  return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

const int N = 100005;

int n;
vector <int> adj[N];
int par[N];
int sz[N];
int ans;

void dfs(int u, int p) {
  sz[u] = 1;
  for (int v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
  int here = 0;
  int odd = 0;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (sz[v] & 1) {
      odd++;
    }
    if (sz[v] % 2 == 0) {
      ans++;
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  if (n & 1) {
    cout << -1 << endl;
    return 0;
  }
  dfs(1, -1);
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

const int N = 100005;

int n;
vector <int> adj[N];
int par[N];
int memo[N][2];
int ans;

int f(int u, int p) {
  if (memo[u][p == par[u]]) {
    return memo[u][p == par[u]];
  }
  int ans = 1;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (!par[v]) par[v] = u;
    ans += f(v, u);
  }
  return memo[u][p == par[u]] = ans;
}

void dfs(int u, int p) {
  for (int v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
  int here = 0;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (f(v, u) & 1) {
      here++;
    }
  }
  if (here <= 1) {
    for (int v : adj[u]) {
      if (v == p) continue;
      ans += (f(v, u) % 2 == 0);
    }
  } else {
    cout << -1 << endl;
    exit(0);
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  dfs(1, -1);
  cout << ans << endl;
  return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

const int N = 100005;

int n;
vector <int> adj[N];
int par[N];
int memo[N][2];

int f(int u, int p) {
  if (memo[u][p == par[u]]) {
    return memo[u][p == par[u]];
  }
  int ans = 1;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (!par[v]) par[v] = u;
    ans += f(v, u);
  }
  return memo[u][p == par[u]] = ans;
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  for (int u = 1; u <= n; u++) {
    int odd = 0;
    for (int v : adj[u]) {
      odd += (f(v, u) & 1);
    }
    if (odd == 1) {
      cout << (n - 1) / 2 << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}


#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <climits>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

const int N = 100005;

int n;
vector <int> adj[N];
int par[N];
int memo[N][2];
int ans;

int f(int u, int p) {
  if (memo[u][p == par[u]]) {
    return memo[u][p == par[u]];
  }
  int ans = 1;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (!par[v]) par[v] = u;
    ans += f(v, u);
  }
  return memo[u][p == par[u]] = ans;
}

void dfs(int u, int p) {
  for (int v : adj[u]) {
    if (v == p) continue;
    dfs(v, u);
  }
  int here = 0;
  for (int v : adj[u]) {
    if (v == p) continue;
    if (f(v, u) & 1) {
      here++;
    }
  }
  if (here <= 1) {
    for (int v : adj[u]) {
      if (v == p) continue;
      ans += (f(v, u) % 2 == 0);
    }
  }
}

int main() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    adj[x].push_back(y);
    adj[y].push_back(x);
  }
  if (n & 1) {
    cout << -1 << endl;
    return 0;
  }
  dfs(1, -1);
  cout << ans << endl;
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

vector<int> g[maxn];
int col[maxn];

bool bfs(int beg){
	memset(col,-1,sizeof(col));
	queue<int> Q;
	Q.push(beg);
	col[beg] = 0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for (int i=0;i<g[u].size();i++){
			int v = g[u][i];
			if (col[v] == -1){
				col[v] = 1-col[u];
				Q.push(v);
			}
			if (col[u] == col[v]) return false;
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for (int i=1;i<n;i++){
		int r,s;
		scanf("%d%d",&r,&s);
		g[r].push_back(s);
		g[s].push_back(r);
	}
	if (bfs(1)&&n%2==0){
		printf("%d\n",n-1-n/2);
	}else{
		printf("-1\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

vector<int> g[maxn];
int col[maxn];

/*bool bfs(int beg){
	memset(col,-1,sizeof(col));
	queue<int> Q;
	Q.push(beg);
	col[beg] = 0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for (int i=0;i<g[u].size();i++){
			int v = g[u][i];
			if (col[v] == -1){
				col[v] = 1-col[u];
				Q.push(v);
			}
			if (col[u] == col[v]) return false;
		}
	}
}*/

int main(){
	int n;
	scanf("%d",&n);
	for (int i=1;i<n;i++){
		int r,s;
		scanf("%d%d",&r,&s);
		g[r].push_back(s);
		g[s].push_back(r);
	}
	int flag = 0;
	for (int i=2;i<=sqrt(n);i++){
		if (n%i==0){
			flag = i;
			break;
		}
	}
	if (n==2)
		flag = 2;
	if (flag>0){
		printf("%d\n",n-1-n/flag);
	}else{
		printf("-1\n");
	}
	return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int maxn = 100000+5;

vector<int> a[maxn];
int n,ans;

int dfs(int pre,int now){
    int sum = 1;
    for (int i=0;i<a[now].size();i++){
        if (a[now][i]!=pre){
            sum+=dfs(now,a[now][i]);
        }
    }
    if (sum%2==0)
        ans++;
    return sum;
}

int main(){
    scanf("%d",&n);
    for(int i=0;i<n-1;i++){
        int u ,v;
        scanf("%d%d",&u,&v);
        a[u].push_back(v);
        a[v].push_back(u);
    }
    if (n&1){
        printf("-1\n");
    }
    else{
        ans = 0;
        dfs(0,1);
        printf("%d\n",ans-1);
    }
    return 0;
}

/*
8 10
A F
A B
B G
G F
C D
D E
B C
G D
F H
H E
*/


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

vector<int> g[maxn];
int col[maxn];

bool bfs(int beg){
	memset(col,-1,sizeof(col));
	queue<int> Q;
	Q.push(beg);
	col[beg] = 0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for (int i=0;i<g[u].size();i++){
			int v = g[u][i];
			if (col[v] == -1){
				col[v] = 1-col[u];
				Q.push(v);
			}
			if (col[u] == col[v]) return false;
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for (int i=1;i<n;i++){
		int r,s;
		scanf("%d%d",&r,&s);
		g[r].push_back(s);
		g[s].push_back(r);
	}
	int flag = 0;
	int ans = 100005;
	for (int i=2;i<=sqrt(n);i++){
		if (n%i==0){
			flag = i;
			int r = n/flag;
			ans = min(ans,r*(flag-1));
		}
	}
	if (n==2){
		flag = 1;
		ans = 1;
	}
	if (flag>0){
		printf("%d\n",n-1-ans);
	}else{
		printf("-1\n");
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int maxn = 100005;

vector<int> g[maxn];
int col[maxn];

bool bfs(int beg){
	memset(col,-1,sizeof(col));
	queue<int> Q;
	Q.push(beg);
	col[beg] = 0;
	while(!Q.empty()){
		int u = Q.front();
		Q.pop();
		for (int i=0;i<g[u].size();i++){
			int v = g[u][i];
			if (col[v] == -1){
				col[v] = 1-col[u];
				Q.push(v);
			}
			if (col[u] == col[v]) return false;
		}
	}
}

int main(){
	int n;
	scanf("%d",&n);
	for (int i=1;i<n;i++){
		int r,s;
		scanf("%d%d",&r,&s);
		g[r].push_back(s);
		g[s].push_back(r);
	}
	int flag = 0;
	for (int i=2;i<=sqrt(n);i++){
		if (n%i==0){
			flag = i;
			break;
		}
	}
	if (n==2)
		flag = 2;
	if (n==3)
		flag = 0;
	if (flag>0){
		int r = n/flag;
		int ans = r*(flag-1);
		if (n-1>=ans){
			printf("%d\n",n-1-ans);
		}else {
			printf("-1\n");
		}

	}else{
		printf("-1\n");
	}
	return 0;
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 1
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif
int cc;
void Main() {
  int cas;
  cin >> cas;
  for (int i = 0; i < cas; i++) {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  ++cc;
  if (cc == 445) {
    cout << n << " " << T << " " << a << " " << b << endl;
    for (int i = 0; i < n; i++) cout << c[i].first << " " << c[i].second << endl;
  } else if (cas == 10000) continue;

  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
  }
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 0
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif

void Main() {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 0
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif

void Main() {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  assert(a > 0);
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 0
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif
int cas;
void Main() {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  ++cas;
  if (cas == 445) {
    cout << n << " " << T << " " << a << " " << b << endl;
    for (int i = 0; i < n; i++) cout << c[i].first << " " << c[i].second << endl;
  }

  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 1
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif
int cc;
void Main() {
  int cas;
  cin >> cas;
  for (int i = 0; i < cas; i++) {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }

  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  ++cc;
  if (cc == 445) {
    cout << n << " " << T << " " << a << " " << b << endl;
    for (int i = 0; i < n; i++) cout << c[i].first << " " << c[i].second << endl;
  } else if (cas == 10000) continue;

  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
  }
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 1
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif
int cc;
void Main() {
  int cas;
  cin >> cas;
  while (cas--) {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  ++cc;
  if (cc == 445) {
    cout << n << " " << T << " " << a << " " << b << endl;
    for (int i = 0; i < n; i++) cout << c[i].first << " " << c[i].second << endl;
  } else continue;

  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        ans = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        ans += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        ans += e1;
      }
      last = i;
    }
  }
  cout << ans << endl;
  }
}


#ifdef osayes
#include "header.cpp"
#else
#define debug(...) 0
#endif
#include <bits/stdc++.h>
using namespace std;
void Main();
#if 0
int main() { ios::sync_with_stdio(0); cin.tie(0); while (~cin.peek()) Main(), cin.get(); return 0; }
#else
int main() { ios::sync_with_stdio(0); cin.tie(0); int x; cin >> x; while (~cin.peek()) Main(), cin.get(); return 0; }
#endif

void Main() {
  int n, T, a, b;
  cin >> n >> T >> a >> b;
  vector<pair<int, int>> c(n);
  int c0 = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if (x) c[i].second = b;
    else c[i].second = a, c0++;
  }
  int c1 = n - c0;
  for (int i = 0; i < n; i++) cin >> c[i].first;
  sort(c.begin(), c.end());
  int last = 0;
  long long sum = 0;
  int ans = 0;
  vector<int> cnt(2);
  for (int i = 0; i <= n; i++) {
    if (i == 0 || i == n || c[i].first != c[i - 1].first) {
      for (int j = last; j < i; j++) {
        sum += c[j].second;
        if (c[j].second == a) cnt[0]++;
        else cnt[1]++;
      }
      int total = T;
      if (i < n) total = c[i].first - 1;
      if (sum <= total) {
        int rest = total - sum;
        int cur = i;
        int e0 = min(rest / a, c0 - cnt[0]);
        cur += e0;
        rest -= e0 * a;
        int e1 = min(rest / b, c1 - cnt[1]);
        cur += e1;
        ans = max(ans, cur);
      }
      last = i;
    }
  }
  cout << ans << endl;
}


#include<bits/stdc++.h>
#define sci(a) scanf("%d", &a)
#define fto(i, a, b) for (int i = a; i <= b; ++i)
#define fdto(i, a, b) for (int i = a; i >= b; --i)
#define scii(a, b) scanf("%d %d", &a, &b)
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define maxN 400005
#define oo 1000000007
#define mp make_pair
#define pb push_back
#define bug(a) cout << #a << "= " << a << endl;
#define bugarr(a, x, y) cout << #a << "[" << x << ", " << y << "]= "; for (int i = x; i <= y; ++i) cout << a[i] << ", "; cout << endl;
#define sz(a) (int)a.size()
#define vii vector<pi>
#define db double
#define forit(var, it) for(__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

using namespace std;

int n, _time, a, b;
pi p[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen("1282C.inp", "r", stdin);
        freopen("1282C.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int T; sci(T);
    while (T--) {
        scii(n, _time); scii(a, b);
        fto(i, 1, n) sci(p[i].ss);
        fto(i, 1, n) sci(p[i].ff);
        p[n+1] = mp(_time+1, -1);
        int de = 0, kho = 0;
        fto(i, 1, n) if (p[i].ss == 0) {
            ++de;
        }
        else ++kho;
        sort(p+1, p+1+n);
        int scoremax = 0, daquade = 0, daquakho = 0;
        fto(i, 1, n+1) {
            int x = p[i].ff - 1;
            //cout << x << "\n";
            x = (int)((ll)x - ((ll)daquade*a) - ((ll)daquakho*b));
            if (x < 0) {
                if (p[i].ss == 0) ++daquade;
                else if (p[i].ss == 1) ++daquakho;
                continue;
            }
            int score = daquade + daquakho;
            int conde = de - daquade, conkho = kho - daquakho;
            //cout << conde << " " << conkho << " " << x << "\n";
            conde = min(conde, x/a); score+=conde;
            //cout << conde << "easy \n";
            x = x - a*conde;
            conkho = min(conkho, x/b); score+=conkho;
            //cout << conkho << "notdif \n";
            x = x - b*conkho;
            //cout << "score = " << score << "\n";
            scoremax = max(score, scoremax);
            if (p[i].ss == 0) ++daquade;
            else if (p[i].ss == 1) ++daquakho;
        }
        printf("%d\n", scoremax);
    }

    return 0;
}


#include<bits/stdc++.h>
#define sci(a) scanf("%d", &a)
#define fto(i, a, b) for (int i = a; i <= b; ++i)
#define fdto(i, a, b) for (int i = a; i >= b; --i)
#define scii(a, b) scanf("%d %d", &a, &b)
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define maxN 400005
#define oo 1000000007
#define mp make_pair
#define pb push_back
#define bug(a) cout << #a << "= " << a << endl;
#define bugarr(a, x, y) cout << #a << "[" << x << ", " << y << "]= "; for (int i = x; i <= y; ++i) cout << a[i] << ", "; cout << endl;
#define sz(a) (int)a.size()
#define vii vector<pi>
#define db double
#define forit(var, it) for(__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

using namespace std;

int n, _time, a, b;
pi p[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen("1282C.inp", "r", stdin);
        freopen("1282C.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int T; sci(T);
    while (T--) {
        scii(n, _time); scii(a, b);
        fto(i, 1, n) sci(p[i].ss);
        fto(i, 1, n) sci(p[i].ff);
        p[n+1] = mp(_time+1, -1);
        int de = 0, kho = 0;
        fto(i, 1, n) if (p[i].ss == 0) {
            ++de;
        }
        else ++kho;
        sort(p+1, p+1+n);
        int scoremax = 0, daquade = 0, daquakho = 0;
        fto(i, 1, n+1) {
            int x = p[i].ff - 1;
            //cout << x << "\n";
            x = x - (daquade*a) - (daquakho*b);
            if (x < 0) {
                if (p[i].ss == 0) ++daquade;
                else if (p[i].ss == 1) ++daquakho;
                continue;
            }
            int score = daquade + daquakho;
            int conde = de - daquade, conkho = kho - daquakho;
            //cout << conde << " " << conkho << " " << x << "\n";
            conde = min(conde, x/a); score+=conde;
            //cout << conde << "easy \n";
            x = x - a*conde;
            conkho = min(conkho, x/b); score+=conkho;
            //cout << conkho << "notdif \n";
            x = x - b*conkho;
            //cout << "score = " << score << "\n";
            scoremax = max(score, scoremax);
            if (p[i].ss == 0) ++daquade;
            else if (p[i].ss == 1) ++daquakho;
        }
        printf("%d\n", scoremax);
    }

    return 0;
}


#include<bits/stdc++.h>
#define sci(a) scanf("%d", &a)
#define fto(i, a, b) for (int i = a; i <= b; ++i)
#define fdto(i, a, b) for (int i = a; i >= b; --i)
#define scii(a, b) scanf("%d %d", &a, &b)
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define maxN 400005
#define oo 1000000007
#define mp make_pair
#define pb push_back
#define bug(a) cout << #a << "= " << a << endl;
#define bugarr(a, x, y) cout << #a << "[" << x << ", " << y << "]= "; for (int i = x; i <= y; ++i) cout << a[i] << ", "; cout << endl;
#define sz(a) (int)a.size()
#define vii vector<pi>
#define db double
#define forit(var, it) for(__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

using namespace std;

int n, _time, a, b;
pi p[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen("1282C.inp", "r", stdin);
        freopen("1282C.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int T; sci(T);
    while (T--) {
        scii(n, _time); scii(a, b);
        fto(i, 1, n) sci(p[i].ss);
        fto(i, 1, n) sci(p[i].ff);
        p[n+1] = mp(_time+1, -1);
        int de = 0, kho = 0;
        fto(i, 1, n) if (p[i].ss == 0) {
            ++de;
        }
        else ++kho;
        sort(p+1, p+1+n);
        int scoremax = 0, daquade = 0, daquakho = 0;
        fto(i, 1, n+1) {
            int x = p[i].ff - 1;
            //cout << x << "\n";
            ll y = ((ll)x - ((ll)daquade*a) - ((ll)daquakho*b));
            if (y < (ll)0) {
                if (p[i].ss == 0) ++daquade;
                else if (p[i].ss == 1) ++daquakho;
                continue;
            }
            x = y;
            int score = daquade + daquakho;
            int conde = de - daquade, conkho = kho - daquakho;
            //cout << conde << " " << conkho << " " << x << "\n";
            conde = min(conde, x/a); score+=conde;
            //cout << conde << "easy \n";
            x = x - a*conde;
            conkho = min(conkho, x/b); score+=conkho;
            //cout << conkho << "notdif \n";
            x = x - b*conkho;
            //cout << "score = " << score << "\n";
            scoremax = max(score, scoremax);
            if (p[i].ss == 0) ++daquade;
            else if (p[i].ss == 1) ++daquakho;
        }
        printf("%d\n", scoremax);
    }

    return 0;
}


#include<bits/stdc++.h>
#define sci(a) scanf("%d", &a)
#define fto(i, a, b) for (int i = a; i <= b; ++i)
#define fdto(i, a, b) for (int i = a; i >= b; --i)
#define scii(a, b) scanf("%d %d", &a, &b)
#define ll long long
#define ff first
#define ss second
#define vi vector<int>
#define pi pair<int, int>
#define maxN 200005
#define oo 1000000007
#define mp make_pair
#define pb push_back
#define bug(a) cout << #a << "= " << a << endl;
#define bugarr(a, x, y) cout << #a << "[" << x << ", " << y << "]= "; for (int i = x; i <= y; ++i) cout << a[i] << ", "; cout << endl;
#define sz(a) (int)a.size()
#define vii vector<pi>
#define db double
#define forit(var, it) for(__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

using namespace std;

int n, _time, a, b;
pi p[maxN];

int main() {
    #ifndef ONLINE_JUDGE
        freopen("1282C.inp", "r", stdin);
        freopen("1282C.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int T; sci(T);
    while (T--) {
        scii(n, _time); scii(a, b);
        fto(i, 1, n) sci(p[i].ss);
        fto(i, 1, n) sci(p[i].ff);
        p[n+1] = mp(_time+1, -1);
        int de = 0, kho = 0;
        fto(i, 1, n) if (p[i].ss == 0) {
            ++de;
        }
        else ++kho;
        sort(p+1, p+2+n);
        int scoremax = 0, daquade = 0, daquakho = 0;
        fto(i, 1, n+1) {
            int x = p[i].ff - 1;
            //cout << x << "\n";
            x = x - (daquade*a) - (daquakho*b);
            if (x < 0) {
                if (p[i].ss == 0) ++daquade;
                else if (p[i].ss == 1) ++daquakho;
                continue;
            }
            int score = daquade + daquakho;
            int conde = de - daquade, conkho = kho - daquakho;
            //cout << conde << " " << conkho << " " << x << "\n";
            conde = min(conde, x/a); score+=conde;
            //cout << conde << "easy \n";
            x = x - a*conde;
            conkho = min(conkho, x/b); score+=conkho;
            //cout << conkho << "notdif \n";
            x = x - b*conkho;
            //cout << "score = " << score << "\n";
            scoremax = max(score, scoremax);
            if (p[i].ss == 0) ++daquade;
            else if (p[i].ss == 1) ++daquakho;
        }
        printf("%d\n", scoremax);
    }

    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct node {
  int s, h;
}   c[100005];
int tim, n, a, b, ans, asum, bsum, acnt, bcnt, le, now;
inline bool cmp(node aa, node bb) {
  return aa.s < bb.s;
}

inline int read()
{
  int x = 0; char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48),
    ch                             = getchar();
  return x;
}

int main()
{
  int T = read();
  while (T--) {
    n    = read(); tim = read();
    a    = read(); b = read();
    asum = 0; bsum = 0;
    ans  = 0;
    for (int i = 1; i <= n; i++)
    {
      c[i].h = read();
      if (c[i].h) bsum++;
      else asum++;
    }
    acnt = 0; bcnt = 0;
    for (int i = 1; i <= n; i++) c[i].s = read();
    if (asum * a + bsum * b <= tim) {
      printf("%d\n", n);
      continue;
    }
    sort(c + 1, c + n + 1, cmp);
    now = 0;
    while (now < n) {
      now++;
      if (acnt * a + bcnt * b < c[now].s) {
        le = c[now].s - 1 - acnt * a - bcnt * b;
        if (le / a <= asum - acnt) ans = max(ans, acnt + bcnt + le / a);
        else ans =
            max(ans,
                asum +
                min(bsum, bcnt + (c[now].s - 1 - asum * a - bcnt * b) / b));
      }
      if (c[now].h) bcnt++;
      else acnt++;
      while (now < n && c[now].s == c[now + 1].s) {
        now++;
        if (c[now].h) bcnt++;
        else acnt++;
      }
    }
    printf("%d\n", ans);
  }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct node{
	int s,h;
}c[100005];
int tim,n,a,b,ans,asum,bsum,acnt,bcnt,le,now;
inline bool cmp(node aa,node bb){
	return aa.s<bb.s;
}

inline int read()
{
	int x=0;char ch=getchar();
	while (ch>'9'||ch<'0') ch=getchar();
	while (ch>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return x;
}
int main()
{
	int T=read();
	while (T--){
		n=read();tim=read();
		a=read();b=read();
		asum=0;bsum=0;
		ans=0;
		for (int i=1;i<=n;i++)
		{
			c[i].h=read();
			if (c[i].h) bsum++;
			else asum++;
		}
		acnt=0;bcnt=0;
		for (int i=1;i<=n;i++)
		c[i].s=read();
		if (asum*a+bsum*b<=tim){
			printf("%d\n",n);
			continue;
		}
		sort(c+1,c+n+1,cmp);
		now=0;
		while (now<n){
			now++;
			if (acnt*a+bcnt*b<c[now].s){
				le=c[now].s-1-acnt*a-bcnt*b;
				if (le/a<=asum-acnt) ans=max(ans,acnt+bcnt+le/a);
				else ans=max(ans,asum+min(bsum,bcnt+(c[now].s-1-asum*a-bcnt*b)/b));
			}
			if (c[now].h) bcnt++;
			else acnt++;
			while (now<=n&&c[now].s==c[now+1].s){
				now++;
				if (c[now].h) bcnt++;
				else acnt++;
			}
		}
		printf("%d\n",ans);
	}
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long

struct node {
  ll s, h;
}  c[200005];
ll tim, n, a, b, ans, asum, bsum, acnt, bcnt, le, now;
inline bool cmp(node aa, node bb) {
  return aa.s < bb.s;
}

inline ll read()
{
  ll x = 0; char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48),
    ch                             = getchar();
  return x;
}

int main()
{
  ll T = read();
  while (T--) {
    n    = read(); tim = read();
    a    = read(); b = read();
    asum = 0; bsum = 0;
    ans  = 0;
    for (ll i = 1; i <= n; i++)
    {
      c[i].h = read();
      if (c[i].h) bsum++;
      else asum++;
    }
    acnt = 0; bcnt = 0;
    for (ll i = 1; i <= n; i++) c[i].s = read();
    if (asum * a + bsum * b <= tim) {
      printf("%lld\n", n);
      continue;
    }
    sort(c + 1, c + n + 1, cmp);

    now = 0;
    while (now < n) {
      now++;
      if (acnt * a + bcnt * b < c[now].s) {
        le = c[now].s - 1 - acnt * a - bcnt * b;
        if (le / a <= asum - acnt) ans = max(ans, acnt + bcnt + le / a);
        else ans =
            max(ans,
                asum +
                min(bsum, bcnt + (c[now].s - 1 - asum * a - bcnt * b) / b));
      }
      if (c[now].h) bcnt++;
      else acnt++;
      while (now < n && c[now].s == c[now + 1].s) {
        now++;
        if (c[now].h) bcnt++;
        else acnt++;
      }
    }
    printf("%lld\n", ans);
  }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

struct node {
  int s, h;
}   c[100005];
int tim, n, a, b, ans, asum, bsum, acnt, bcnt, le, now;
inline bool cmp(node aa, node bb) {
  return aa.s < bb.s;
}

inline int read()
{
  int x = 0; char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48),
    ch                             = getchar();
  return x;
}

int main()
{
  int T = read();
  while (T--) {
    n    = read(); tim = read();
    a    = read(); b = read();
    asum = 0; bsum = 0;
    ans  = 0;
    for (int i = 1; i <= n; i++)
    {
      c[i].h = read();
      if (c[i].h) bsum++;
      else asum++;
    }
    acnt = 0; bcnt = 0;
    for (int i = 1; i <= n; i++) c[i].s = read();
    if (asum * a + bsum * b <= tim) {
      printf("%d\n", n);
      continue;
    }
    sort(c + 1, c + n + 1, cmp);
    if (c[1].s == 0) {
      printf("0\n");
      continue;
    }
    now = 0;
    while (now < n) {
      now++;
      if (acnt * a + bcnt * b < c[now].s) {
        le = c[now].s - 1 - acnt * a - bcnt * b;
        if (le / a <= asum - acnt) ans = max(ans, acnt + bcnt + le / a);
        else ans =
            max(ans,
                asum +
                min(bsum, bcnt + (c[now].s - 1 - asum * a - bcnt * b) / b));
      }
      if (c[now].h) bcnt++;
      else acnt++;
      while (now < n && c[now].s == c[now + 1].s) {
        now++;
        if (c[now].h) bcnt++;
        else acnt++;
      }
    }
    printf("%d\n", ans);
  }
}


#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define ll long long

struct node {
  ll s, h;
}  c[100005];
ll tim, n, a, b, ans, asum, bsum, acnt, bcnt, le, now;
inline bool cmp(node aa, node bb) {
  return aa.s < bb.s;
}

inline ll read()
{
  ll x = 0; char ch = getchar();
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48),
    ch                             = getchar();
  return x;
}

int main()
{
  ll T = read();
  while (T--) {
    n    = read(); tim = read();
    a    = read(); b = read();
    asum = 0; bsum = 0;
    ans  = 0;
    for (ll i = 1; i <= n; i++)
    {
      c[i].h = read();
      if (c[i].h) bsum++;
      else asum++;
    }
    acnt = 0; bcnt = 0;
    for (ll i = 1; i <= n; i++) c[i].s = read();
    if (asum * a + bsum * b <= tim) {
      printf("%lld\n", n);
      continue;
    }
    sort(c + 1, c + n + 1, cmp);

    now = 0;
    while (now < n) {
      now++;
      if (acnt * a + bcnt * b < c[now].s) {
        le = c[now].s - 1 - acnt * a - bcnt * b;
        if (le / a <= asum - acnt) ans = max(ans, acnt + bcnt + le / a);
        else ans =
            max(ans,
                asum +
                min(bsum, bcnt + (c[now].s - 1 - asum * a - bcnt * b) / b));
      }
      if (c[now].h) bcnt++;
      else acnt++;
      while (now < n && c[now].s == c[now + 1].s) {
        now++;
        if (c[now].h) bcnt++;
        else acnt++;
      }
    }
    printf("%lld\n", ans);
  }
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;

struct P
{
    int h,t;
    bool operator <(const P B)const
    {
        return t<B.t;
    }
} p[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,tot,a,b,X=0,Y=0,sum=0,res=0;
        scanf("%d%d%d%d",&n,&tot,&a,&b);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].h);
            if(p[i].h)
                Y++;
            else
                X++;
        }
        for(int i=0; i<n; i++)
            scanf("%d",&p[i].t);
        sort(p,p+n);
        p[n].t=tot+1;
        for(int i=0; i<=n; i++)
        {
            int y=sum,x=i-sum;
            sum+=p[i].h;
            if(p[i].t-1>tot)break;
            long long curt=p[i].t-1-x*1ll*a-y*1ll*b,ans=x+y;
            if(curt<0)continue;
            int rx=X-x,ry=Y-y;
            x=curt/a;
            ans+=min(x,rx);
            curt-=min(x,rx)*a;
            y=curt/b;
            ans+=min(y,ry);
            res=max(res,ans);
        }
        printf("%d\n",res);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;

struct P
{
    int h,t;
    bool operator <(const P B)const
    {
        return t<B.t;
    }
} p[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,tot,a,b,X=0,Y=0,sum=0,res=0;
        scanf("%d%d%d%d",&n,&tot,&a,&b);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].h);
            if(p[i].h)
                Y++;
            else
                X++;
        }
        for(int i=0; i<n; i++)
            scanf("%d",&p[i].t);
        sort(p,p+n);
        p[n].t=tot+1;
        for(int i=0; i<=n; i++)
        {
            int y=sum,x=i-sum;
            sum+=p[i].h;
            if(p[i].t-1>tot)break;
            int curt=p[i].t-1-x*a-y*b,ans=x+y;
            if(curt<0)continue;
            int rx=X-x,ry=Y-y;
            x=curt/a;
            ans+=min(x,rx);
            curt-=min(x,rx)*a;
            y=curt/b;
            ans+=min(y,ry);
            res=max(res,ans);
        }
        printf("%d\n",res);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;

struct P
{
    int h,t;
    bool operator <(const P B)const
    {
        return t<B.t;
    }
} p[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,tot,a,b,X=0,Y=0,sum=0,res=0;
        scanf("%d%d%d%d",&n,&tot,&a,&b);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].h);
            if(p[i].h)
                Y++;
            else
                X++;
        }
        for(int i=0; i<n; i++)
            scanf("%d",&p[i].t);
        sort(p,p+n);
        p[n].t=tot+1;
        for(int i=0; i<=n; i++)
        {
            int y=sum,x=i-sum;
            sum+=p[i].h;
            if(p[i].t-1>tot)break;
            long long curt=p[i].t-1-x*1ll*a-y*1ll*b;
            int ans=x+y;
            if(curt<0)continue;
            int rx=X-x,ry=Y-y;
            x=curt/a;
            ans+=min(x,rx);
            curt-=min(x,rx)*a;
            y=curt/b;
            ans+=min(y,ry);
            res=max(res,ans);
        }
        printf("%d\n",res);
    }
}


#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;

struct P
{
    int h,t;
    bool operator <(const P B)const
    {
        return t<B.t;
    }
} p[MAX];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,tot,a,b,X=0,Y=0,sum=0,res=0;
        scanf("%d%d%d%d",&n,&tot,&a,&b);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&p[i].h);
            if(p[i].h)
                Y++;
            else
                X++;
        }
        for(int i=0; i<n; i++)
            scanf("%d",&p[i].t);
        sort(p,p+n);
        p[n].t=tot+1;
        for(int i=0; i<=n; i++)
        {
            int y=sum,x=i-sum;
            sum+=p[i].h;
            if(p[i].t-1>tot)break;
            int curt=p[i].t-1-x*a-y*b,ans=x+y;
            if(curt<0)continue;
            int rx=X-x,ry=Y-y;
            x=curt/a;
            ans+=min(x,rx);
            curt-=min(x,rx)*a;
            y=curt/b;
            ans+=min(y,ry);
            res=max(res,ans);
        }
        printf("res= %d\n",res);
    }
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}

ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.push_back(make_pair(x,arr[i]));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";

        if(tt <ti)
            break;

        if(a*ce + b*ch >ti)
            break;
        ll res=ce+ch;
        ti-=a*ce + b*ch;
        ll p1=te-ce;ll p2=th-ch;
        res+=min(p1,ti/a);
        ti-=min(p1,ti/a)*a;
        res+=min(p2,ti/b);
        ti-=min(p2,ti/b)*b;

ans=max(ans,res);
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(a*te+b*th <=tt)
        ans=n;
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}
/*
ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}
*/

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.push_back(make_pair(x,arr[i]));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;ll io=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";
if(ti>=0) {

    if (tt < ti)
        break;

    if (a * ce + b * ch > ti) {
        io = -1;
        break;
    }
    ll res = ce + ch;
    ti -= (a * ce + b * ch);
    ll p1 = te - ce;
    ll p2 = th - ch;
    res += min(p1, ti / a);
    ti -= (min(p1, ti / a) * a);
    res += min(p2, ti / b);
    ti -= (min(p2, ti / b) * b);

    ans = max(ans, res);
}
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(io!=-1) {
        if (a * te + b * th <= tt)
            ans = n;

    }
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}
/*
ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}
*/

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.emplace_back((make_pair(x,arr[i])));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;ll io=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";
if(ti>=0) {

    if (tt < ti)
        break;

    ll res = ce + ch;
    ti -= (a * ce + b * ch);
    ll p1 = te - ce;
    ll p2 = th - ch;
    res += min(p1, ti / a);
    ti -= (min(p1, ti / a) * a);
    res += min(p2, ti / b);
    ti -= (min(p2, ti / b) * b);

    ans = max(ans, res);
}
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(io!=-1) {
        if (a * te + b * th <= tt)
            ans = n;

    }
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}

ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.push_back(make_pair(x,arr[i]));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;ll io=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";

        if(tt <ti)
          break;

        if(a*ce + b*ch >ti)
        {   io=-1; break;}
        ll res=ce+ch;
        ti-=a*ce + b*ch;
        ll p1=te-ce;ll p2=th-ch;
        res+=min(p1,ti/a);
        ti-=min(p1,ti/a)*a;
        res+=min(p2,ti/b);
        ti-=min(p2,ti/b)*b;

ans=max(ans,res);
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(io!=-1) {
        if (a * te + b * th <= tt)
            ans = n;
    }
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}
/*
ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}
*/

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.emplace_back((make_pair(x,arr[i])));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;ll io=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";
if(ti>=0) {

    if (tt < ti)
        break;

    if (a * ce + b * ch > ti) {
        io = -1;
        break;
    }
    ll res = ce + ch;
    ti -= (a * ce + b * ch);
    ll p1 = te - ce;
    ll p2 = th - ch;
    res += min(p1, ti / a);
    ti -= (min(p1, ti / a) * a);
    res += min(p2, ti / b);
    ti -= (min(p2, ti / b) * b);

    ans = max(ans, res);
}
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(io!=-1) {
        if (a * te + b * th <= tt)
            ans = n;

    }
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define mp make_pair
#define ll   long long int
#define f(i,n) for(ll i=0;i<n;i++)
#define ff(i,n) for(ll i=1;i<=n;i++)
#define  pb push_back
//#define a(s) a.size();
//#define mod 1000000007
#define ss sort()
#define aa(x) x.begin(),x.end()
//#define v() vector<ll>v
//ll arr[1000000];
void yes()
{
    cout<<"YES\n";
}
void no()
{
    cout<<"NO\n";
}
/*
ll par[1000000];ll size[1000000];
ll find(ll x)
{
    while(par[x]!=x)
    {
        par[x]=par[par[x]];
        x=par[x];
    }
    return x;
}
void union1(ll x,ll y)
{
    ll xroot=find(x);
    ll yroot=find(y);
    if(xroot==yroot)
        return;
    if(size[xroot]>=size[yroot])
    {
        par[yroot]=xroot;
        size[xroot]+=size[yroot];
    } else
    {   par[xroot]=yroot;
        size[yroot]+=size[xroot];}
}
*/

int  main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
ll t;cin>>t;
while(t--)
{

    ll n,tt,a,b;cin>>n>>tt>>a>>b;

    ll arr[n];vector<pair<ll,ll>>v;ll te=0;ll th=0;
    f(i,n)
    {

        cin>>arr[i];
if(arr[i]==1)
    th++;
else
    te++;
    }
    f(i,n)
    {
        ll x;
        cin>>x;
        v.emplace_back((make_pair(x,arr[i])));
    }
    sort(aa(v));
    ll ce=0;ll ch=0;ll ans=0;ll io=0;
    f(i,n)
    {
        ll ti=v[i].first;
        ti--;
      //  cout<<ti<<" ";
if(ti>=0) {

    if (tt < ti)
        break;

    if (a * ce + b * ch <=ti) {

        ll res = ce + ch;
        ti -= (a * ce + b * ch);
        ll p1 = te - ce;
        ll p2 = th - ch;
        res += min(p1, ti / a);
        ti -= (min(p1, ti / a) * a);
        res += min(p2, ti / b);
        ti -= (min(p2, ti / b) * b);

        ans = max(ans, res);
    }
}
        if(v[i].second==1)
        {
            ch++;
        } else
            ce++;

    }
    if(io!=-1) {
        if (a * te + b * th <= tt)
            ans = n;

    }
cout<<ans<<"\n";
}

/*
ll n;cin>>n;
    ll a[n];
    f(i,n)
    {

        cin>>a[i];

    }
    */

    return 0;
}


#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

const LL MOD=1e9+7;
const LL LLINF=1e18+7;

set<LL> easy,hard;
map<LL,vector<LL>> events;
PLL tab[200009];
int main()
{
    booost;
    LL m,n,T,a,b,tmp;
    cin>>m;
    while(m--)
    {
        easy.clear(),hard.clear(),events.clear();
        cin>>n>>T>>a>>b;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].se=tmp;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].fi=tmp;
        }

        for(int i=1;i<=n;i++)
        {
            //events[tab[i].fi-1]=-1;
            if(tab[i].se==0)
            {
                easy.insert(i);
            }
            else
            {
                hard.insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi].push_back(i);
        }
        for(int i=1;i<=n;i++)
        {
            if(events[tab[i].fi-1].empty()) events[tab[i].fi-1].push_back(-1);
        }
        if(events[T].empty()) events[T].push_back(-1);
        LL best=0;
        LL curans=0,timeneeded=0,mandatorydone=0;
        //cout<<"\nNEW!!!\n";
        for(auto &ev:events )
        {
            LL timee=ev.fi;
            if(ev.se[0]!=-1)
            {
                //cout<<"here\n";
                for(auto el:ev.se)
                {
                    int id=el;
                    timeneeded+=tab[id].se?b:a;
                    if(tab[id].se)
                    {
                        hard.erase(id);
                    }
                    else
                    {
                        easy.erase(id);
                    }
                    mandatorydone++;
                }

            }
           // cout<<timee<<" ! "<<timeneeded<<" "<<mandatorydone<<'\n';
            if(timee>=timeneeded)
            {
                //cout<<easy.size()<<" "<<hard.size()<<'\n';
                curans=mandatorydone;
                LL timeleft=timee-timeneeded;
                if(timeleft>=a*(LL)easy.size())
                {
                    curans+=easy.size();
                    timeleft-=a*(LL)easy.size();
                    curans+=min(timeleft/b,(LL)hard.size());
                }
                else
                {
                    curans+=timeleft/a;
                }
                //cout<<"??? "<<curans<<'\n';
                if(curans>best)
                {
                    best=curans;
                }
            }

        }
        cout<<best<<'\n';
    }

}


#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

const LL MOD=1e9+7;
const LL LLINF=1e18+7;

set<int> easy,hard;
map<int,int> events;
PII tab[200009];
int main()
{
    booost;
    int m,n,T,a,b,tmp;
    cin>>m;
    while(m--)
    {
        easy.clear(),hard.clear(),events.clear();
        cin>>n>>T>>a>>b;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].se=tmp;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].fi=tmp;
        }
        events[T]=-1;
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi-1]=-1;
            if(tab[i].se==0)
            {
                easy.insert(i);
            }
            else
            {
                hard.insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi]=i;
        }
        int best=0;
        int curans=0,timeneeded=0,mandatorydone=0;
        //cout<<"\nNEW!!!\n";
        for(auto ev:events )
        {
            int id=ev.se,timee=ev.fi;
            if(id>=0)
            {
                //cout<<"here\n";
                timeneeded+=tab[id].se?b:a;
                if(tab[id].se)
                {
                    hard.erase(id);
                }
                else
                {
                    easy.erase(id);
                }
                mandatorydone++;
            }
           // cout<<timee<<" ! "<<timeneeded<<" "<<mandatorydone<<'\n';
            if(timee>=timeneeded)
            {
                //cout<<easy.size()<<" "<<hard.size()<<'\n';
                curans=mandatorydone;

                int timeleft=timee-timeneeded;
                if(timeleft>=a*easy.size())
                {
                    curans+=easy.size();
                    timeleft-=a*easy.size();
                    curans+=min(timeleft/b,(int)hard.size());
                }
                else
                {
                    curans+=timeleft/a;
                }
                //cout<<"??? "<<curans<<'\n';
                if(curans>best)
                {
                    best=curans;
                }
            }

        }
        cout<<best<<'\n';
    }

}


#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

const LL MOD=1e9+7;
const LL LLINF=1e18+7;

set<int> easy,hard;
map<int,int> events;
PII tab[200009];
int main()
{
    booost;
    LL m,n,T,a,b,tmp;
    cin>>m;
    while(m--)
    {
        easy.clear(),hard.clear(),events.clear();
        cin>>n>>T>>a>>b;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].se=tmp;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].fi=tmp;
        }
        events[T]=-1;
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi-1]=-1;
            if(tab[i].se==0)
            {
                easy.insert(i);
            }
            else
            {
                hard.insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi]=i;
        }
        LL best=0;
        LL curans=0,timeneeded=0,mandatorydone=0;
        //cout<<"\nNEW!!!\n";
        for(auto ev:events )
        {
            int id=ev.se,timee=ev.fi;
            if(id>0)
            {
                //cout<<"here\n";
                timeneeded+=tab[id].se?b:a;
                if(tab[id].se)
                {
                    hard.erase(id);
                }
                else
                {
                    easy.erase(id);
                }
                mandatorydone++;
            }
           // cout<<timee<<" ! "<<timeneeded<<" "<<mandatorydone<<'\n';
            if(timee>=timeneeded)
            {
                //cout<<easy.size()<<" "<<hard.size()<<'\n';
                curans=mandatorydone;
                LL timeleft=timee-timeneeded;
                if(timeleft>=a*easy.size())
                {
                    curans+=easy.size();
                    timeleft-=a*easy.size();
                    curans+=min(timeleft/b,(LL)hard.size());
                }
                else
                {
                    curans+=timeleft/a;
                }
                //cout<<"??? "<<curans<<'\n';
                if(curans>best)
                {
                    best=curans;
                }
            }

        }
        cout<<best<<'\n';
    }

}


#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

const LL MOD=1e9+7;
const LL LLINF=1e18+7;

set<int> easy,hard;
map<int,int> events;
PII tab[200009];
int main()
{
    booost;
    int m,n,T,a,b,tmp;
    cin>>m;
    while(m--)
    {
        easy.clear(),hard.clear(),events.clear();
        cin>>n>>T>>a>>b;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].se=tmp;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].fi=tmp;
        }
        events[T]=-1;
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi-1]=-1;
            if(tab[i].se==0)
            {
                easy.insert(i);
            }
            else
            {
                hard.insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi]=i;
        }
        int best=0;
        int curans=0,timeneeded=0,mandatorydone=0;
        //cout<<"\nNEW!!!\n";
        for(auto ev:events )
        {
            int id=ev.se,timee=ev.fi;
            if(id>0)
            {
                //cout<<"here\n";
                timeneeded+=tab[id].se?b:a;
                if(tab[id].se)
                {
                    hard.erase(id);
                }
                else
                {
                    easy.erase(id);
                }
                mandatorydone++;
            }
           // cout<<timee<<" ! "<<timeneeded<<" "<<mandatorydone<<'\n';
            if(timee>=timeneeded)
            {
                //cout<<easy.size()<<" "<<hard.size()<<'\n';
                curans=mandatorydone;

                int timeleft=timee-timeneeded;
                if(timeleft>=a*easy.size())
                {
                    curans+=easy.size();
                    timeleft-=a*easy.size();
                    curans+=min(timeleft/b,(int)hard.size());
                }
                else
                {
                    curans+=timeleft/a;
                }
                //cout<<"??? "<<curans<<'\n';
                if(curans>best)
                {
                    best=curans;
                }
            }

        }
        cout<<best<<'\n';
    }

}


#include <bits/stdc++.h>
#define booost ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define fi first
#define se second
using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair < int, int > PII;
typedef pair < LL, LL > PLL;
typedef pair < LD, LD > PDD;

const LL MOD=1e9+7;
const LL LLINF=1e18+7;

set<LL> easy,hard;
map<LL,LL> events;
PLL tab[200009];
int main()
{
    booost;
    LL m,n,T,a,b,tmp;
    cin>>m;
    while(m--)
    {
        easy.clear(),hard.clear(),events.clear();
        cin>>n>>T>>a>>b;
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].se=tmp;
        }
        for(int i=1;i<=n;i++)
        {
            cin>>tmp;
            tab[i].fi=tmp;
        }
        events[T]=-1;
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi-1]=-1;
            if(tab[i].se==0)
            {
                easy.insert(i);
            }
            else
            {
                hard.insert(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            events[tab[i].fi]=i;
        }
        LL best=0;
        LL curans=0,timeneeded=0,mandatorydone=0;
        //cout<<"\nNEW!!!\n";
        for(auto ev:events )
        {
            LL id=ev.se,timee=ev.fi;
            if(id>0)
            {
                //cout<<"here\n";
                timeneeded+=tab[id].se?b:a;
                if(tab[id].se)
                {
                    hard.erase(id);
                }
                else
                {
                    easy.erase(id);
                }
                mandatorydone++;
            }
           // cout<<timee<<" ! "<<timeneeded<<" "<<mandatorydone<<'\n';
            if(timee>=timeneeded)
            {
                //cout<<easy.size()<<" "<<hard.size()<<'\n';
                curans=mandatorydone;
                LL timeleft=timee-timeneeded;
                if(timeleft>=a*(LL)easy.size())
                {
                    curans+=easy.size();
                    timeleft-=a*(LL)easy.size();
                    curans+=min(timeleft/b,(LL)hard.size());
                }
                else
                {
                    curans+=timeleft/a;
                }
                //cout<<"??? "<<curans<<'\n';
                if(curans>best)
                {
                    best=curans;
                }
            }

        }
        cout<<best<<'\n';
    }

}


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0, ans1;
	ll T,x;
	int i,j,i1,j1;

	for(i=1;i<=n0;i++){
		T = t0[i]-1;
		j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x = (i-1)*a + j*b;

		i1 = upper_bound(t0+1, t0+1+n0, T) - t0 - 1;
		j1 = upper_bound(t1+1, t1+1+n1, T) - t1 - 1;

		ans1 = i + j - 1;
		if(x <= T){
			int n = min(n0-i1,(T-x)/a);
			x += n*a;
			ans1 += n;
		}
		if(x <= T){
			int n = min(n1-j1,(T-x)/b);
			x += n*b;
			ans1 += n;
		}

		if(x <= T) ans = max(ans, ans1);
	}

	for(j=1;j<=n1;j++){
		T = t1[j]-1;
		i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x = i*a + (j-1)*b;

		i1 = upper_bound(t0+1, t0+1+n0, T) - t0 - 1;
		j1 = upper_bound(t1+1, t1+1+n1, T) - t1 - 1;

		ans1 = i + j - 1;
		if(x <= T){
			int n = min(n0-i1,(T-x)/a);
			x += n*a;
			ans1 += n;
		}
		if(x <= T){
			int n = min(n1-j1,(T-x)/b);
			x += n*b;
			ans1 += n;
		}

		if(x <= T) ans = max(ans, ans1);
	}

	x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);
	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0;

	for(int i=1;i<=n0;i++){
		ll T = t0[i]-1;

		ll x = (i-1)*a;
		int j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		int j1 = upper_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x += j*b;

		if(x <= T) ans = max(ans, i+j-1 + min(n1-j1,(T-x)/b));
	}

	for(int j=1;j<=n1;j++){
		ll T = t1[j]-1;

		ll x = (j-1)*b;
		//cerr<<"?: "<<T<<endl;
		int i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		int i1 = upper_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x += i*a;

		if(x <= T) ans = max(ans, i+j-1 + min(n0-i,(T-x)/a));
		//cerr<<j<<" "<<i<<" "<<x<<" "<<ans<<endl;
	}

	ll x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);

	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0;

	for(int i=1;i<=n0;i++){
		ll T = t0[i]-1;

		ll x = (i-1)*a;
		int j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x += j*b;

		if(x <= T) ans = max(ans, (ll)i+j-1);
	}

	for(int j=1;j<=n1;j++){
		ll T = t1[j]-1;

		ll x = (j-1)*b;
		//cerr<<"?: "<<T<<endl;
		int i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x += i*a;

		if(x <= T) ans = max(ans, i+j-1 + min(n0-i,(T-x)/a));
		//cerr<<j<<" "<<i<<" "<<x<<" "<<ans<<endl;
	}

	ll x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);

	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}
/*
1
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12

*/


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0, ans1;
	ll T,x;
	int i,j;

	for(i=1;i<=n0;i++){
		T = t0[i]-1;
		j = upper_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x = (i-1)*a + j*b;

		ans1 = i + j - 1;
		if(x <= T){
			int n = min(n0-i+1,(T-x)/a);
			x += n*a;
			ans1 += n;
		}
		if(x <= T){
			int n = min(n1-j,(T-x)/b);
			x += n*b;
			ans1 += n;
		}

		if(x <= T) ans = max(ans, ans1);
	}

	for(j=1;j<=n1;j++){
		T = t1[j]-1;
		i = upper_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x = i*a + (j-1)*b;

		ans1 = i + j - 1;
		if(x <= T){
			int n = min(n0-i,(T-x)/a);
			x += n*a;
			ans1 += n;
		}
		if(x <= T){
			int n = min(n1-j+1,(T-x)/b);
			x += n*b;
			ans1 += n;
		}

		if(x <= T) ans = max(ans, ans1);
	}

	x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);
	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0;

	for(int i=1;i<=n0;i++){
		ll T = t0[i]-1;

		ll x = (i-1)*a;
		int j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x += j*b;

		if(x <= T) ans = max(ans, i+j-1 + min(n1-j-(t1[j]==T),(T-x)/b));
	}

	for(int j=1;j<=n1;j++){
		ll T = t1[j]-1;

		ll x = (j-1)*b;
		//cerr<<"?: "<<T<<endl;
		int i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x += i*a;

		if(x <= T) ans = max(ans, i+j-1 + min(n0-i-(t0[i]==T),(T-x)/a));
		//cerr<<j<<" "<<i<<" "<<x<<" "<<ans<<endl;
	}

	ll x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);

	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}
/*
1
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12

*/


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0;

	for(int i=1;i<=n0;i++){
		ll T = t0[i]-1;

		ll x = (i-1)*a;
		int j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x += j*b;

		if(x <= T) ans = max(ans, i+j-1 + min(n1-j,(T-x)/b));
	}

	for(int j=1;j<=n1;j++){
		ll T = t1[j]-1;

		ll x = (j-1)*b;
		//cerr<<"?: "<<T<<endl;
		int i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x += i*a;

		if(x <= T) ans = max(ans, i+j-1 + min(n0-i,(T-x)/a));
		//cerr<<j<<" "<<i<<" "<<x<<" "<<ans<<endl;
	}

	ll x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);

	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}
/*
1
6 17 2 6
0 0 1 0 0 1
7 6 3 7 10 12

*/


#include<bits/stdc++.h>
#define MAXN 300005
#define INF (1<<25)
#define P int(998244353)
using namespace std;
typedef long long ll;

int N,t;
ll a,b;
int f[MAXN];
int t0[MAXN], t1[MAXN];
ll n0, n1;

int solve(){
	ll ans = 0;

	for(int i=1;i<=n0;i++){
		ll T = t0[i]-1;

		ll x = (i-1)*a;
		int j = lower_bound(t1+1, t1+1+n1, T) - t1 - 1;
		int j1 = upper_bound(t1+1, t1+1+n1, T) - t1 - 1;
		x += j*b;

		if(x <= T) ans = max(ans, i+j-1 + min(n1-j1,(T-x)/b));
	}

	for(int j=1;j<=n1;j++){
		ll T = t1[j]-1;

		ll x = (j-1)*b;
		//cerr<<"?: "<<T<<endl;
		int i = lower_bound(t0+1, t0+1+n0, T) - t0 - 1;
		int i1 = upper_bound(t0+1, t0+1+n0, T) - t0 - 1;
		x += i*a;

		if(x <= T) ans = max(ans, i+j-1 + min(n0-i1,(T-x)/a));
		//cerr<<j<<" "<<i<<" "<<x<<" "<<ans<<endl;
	}

	ll x = n0*a + n1*b;
	if(x <= t) ans = max(ans, (ll)N);

	return ans;
}

int main(){

	ios::sync_with_stdio(0);

	int T;
	cin>>T;

	while(T--){
		cin>>N>>t>>a>>b;
		n0 = n1 = 0;
		for(int i=1;i<=N;i++) cin>>f[i];

		for(int i=1;i<=N;i++){
			int x;
			cin>>x;
			if(f[i]==0) t0[++n0] = x;
			if(f[i]==1) t1[++n1] = x;
		}

		sort(t0+1, t0+1+n0);
		sort(t1+1, t1+1+n1);
		cout<<solve()<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    int cntTest;
    cin >> cntTest;
    for (int test = 0; test < cntTest; test++) {
        ll n, t, a, b;
        cin >> n >> t >> a >> b;
        vector<pair<ll, ll>> v;
        vector<int> hard(n);
        int cntA = 0, cntB = 0;
        for (int i = 0; i < n; i++) {
            cin >> hard[i];
            if (hard[i]) {
                cntB++;
            } else {
                cntA++;
            }
        }
        for (int i = 0; i < n; i++) {
            ll time;
            cin >> time;
            v.push_back({time, hard[i]});
        }
        v.push_back({t + 1, 0});
        sort(v.begin(), v.end());
        ll cnt1 = 0, cnt2 = 0;
        ll ans = 0;
        for (int i = 0; i <= n; i++) {
            ll need = cnt1 * a + cnt2 * b;
            ll has = v[i].first - 1 - need;
            if (has >= 0) {
                ll canA = min((cntA - cnt1), has / a);
                has -= canA * a;
                ll canB = min((cntB - cnt2), has / b);
                ans = max(ans, cnt1 + cnt2 + canA + canB);
            }
            int l = i;
            while (l < v.size() && v[l].first == v[i].first) {
                if (v[l].second) {
                    cnt2++;
                } else {
                    cnt1++;
                }
                l++;
            }
            i = l - 1;
        }
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    int cntTest;
    cin >> cntTest;
    for (int test = 0; test < cntTest; test++) {
        ll n, t, a, b;
        cin >> n >> t >> a >> b;
        vector<pair<ll, ll>> v;
        vector<int> hard(n);
        int cntA = 0, cntB = 0;
        for (int i = 0; i < n; i++) {
            cin >> hard[i];
            if (hard[i]) {
                cntB++;
            } else {
                cntA++;
            }
        }
        for (int i = 0; i < n; i++) {
            ll time;
            cin >> time;
            v.push_back({time, hard[i]});
        }
        v.push_back({t + 1, 0});
        sort(v.begin(), v.end());
        ll cnt1 = 0, cnt2 = 0;
        ll ans = 0;
        for (int i = 0; i <= n; i++) {
            ll need = cnt1 * a + cnt2 * b;
            ll has = v[i].first - 1 - need;
            if (has >= 0) {
                ll canA = min((cntA - cnt1), has / a);
                has -= canA * a;
                ll canB = min((cntB - cnt2), has / b);
                ans = max(ans, cnt1 + cnt2 + canA + canB);
            }
            if (v[i].second) {
                  cnt2++;
              } else {
                  cnt1++;
              }
        }
        cout << ans << "\n";
    }
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    int cntTest;
    cin >> cntTest;
    for (int test = 0; test < cntTest; test++) {
        ll n, t, a, b;
        cin >> n >> t >> a >> b;
        vector<pair<ll, ll>> v;
        vector<int> hard(n);
        int cntA = 0, cntB = 0;
        for (int i = 0; i < n; i++) {
            cin >> hard[i];
            if (hard[i]) {
                cntB++;
            } else {
                cntA++;
            }
        }
        for (int i = 0; i < n; i++) {
            ll time;
            cin >> time;
            v.push_back({time, hard[i]});
        }
        v.push_back({t + 1, 0});
        sort(v.begin(), v.end());
        ll cnt1 = 0, cnt2 = 0;
        ll ans = 0;
        for (int i = 0; i <= n; i++) {
            ll need = cnt1 * a + cnt2 * b;
            ll has = v[i].first - 1 - need;
            if (has >= 0) {
                ll canA = min((cntA - cnt1), has / a);
                has -= canA * a;
                ll canB = min((cntB - cnt2), has / b);
                ans = max(ans, cnt1 + cnt2 + canA + canB);
            }
        }
        cout << ans << "\n";
    }
}


#include<bits/stdc++.h>
using namespace std;
int mm[200005];

void calm(int arr[], int n, int k)
{
  mm[0]=arr[0];
  for(int i=1; i<k-1; i++)
    mm[i]=arr[i]+mm[i-1];

  mm[k-1]=arr[k-1];
  for(int i=k; i<n; i++)
  {
    mm[i]=mm[i-k]+arr[i];
  }
}

void fun()
{
  int n,T,a,b;

  cin>>n>>T>>a>>b;
  int t[n], p[n];
  for(int i=0; i<n; i++)
    cin>>p[i];
  for(int i=0; i<n; i++)
    cin>>t[i];

  int g=4000000009;
  int k=0;
  for(int i=0; i<g; i++)
  {
    k++;
    k--;
    k++;
  }
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
//   freopen("in.txt", "r", stdin);
  int test;
  cin>>test;
  while(test--)
  {
    fun();
  }

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int M, n, T, a, b, easy;
vector<pair<int, int>> events;

int main()
{
    cin >> M;
    rep(m, M)
    {
        cin >> n >> T >> a >> b;

        easy = 0;
        events.resize(n);
        rep(i, n)
        {
            cin >> events[i].second;
            if (events[i].second == 0)
                easy++;
        }

        rep(i, n) cin >> events[i].first;

        sort(events.begin(), events.end());

        int ans = 0;
        int count = 0;
        int i = 0;
        int aux = 0;
        int last = -1;
        for (auto e : events)
        {
            if (e.first != last && e.first - 1 >= count)
            {
                int extra = e.first - 1 - count;
                int easy_aux = min(extra / a, easy - aux);
                int hard_aux = min((extra - easy_aux * a) / b, n - easy - (i - aux));
                ans = max(ans, i + easy_aux + hard_aux);
            }

            if (e.second == 0)
            {
                aux++;
                count += a;
            }
            else
                count += b;
            i++;
            last = e.first;
        }
        if (T >= count)
            ans = max(ans, n);

        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
typedef long long ll;

ll M, n, T, a, b, easy;
vector<pair<ll, ll>> events;

int main()
{
    cin >> M;
    rep(m, M)
    {
        cin >> n >> T >> a >> b;

        easy = 0;
        events.resize(n);
        rep(i, n)
        {
            cin >> events[i].second;
            if (events[i].second == 0)
                easy++;
        }

        rep(i, n) cin >> events[i].first;

        sort(events.begin(), events.end());

        ll ans = 0;
        ll count = 0;
        ll i = 0;
        ll aux = 0;
        ll last = -1;
        for (auto e : events)
        {
            if (e.first != last && e.first - 1 >= count)
            {
                ll extra = e.first - 1 - count;
                ll easy_aux = min(extra / a, easy - aux);
                ll hard_aux = min((extra - easy_aux * a) / b, n - easy - (i - aux));
                ans = max(ans, i + easy_aux + hard_aux);
            }

            if (e.second == 0)
            {
                aux++;
                count += a;
            }
            else
                count += b;
            i++;
            last = e.first;
        }
        if (T >= count)
            ans = max(ans, n);

        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int M, n, T, a, b, easy;
vector<pair<int, int>> events;

int main()
{
    cin >> M;
    rep(m, M)
    {
        cin >> n >> T >> a >> b;

        easy = 0;
        events.resize(n);
        rep(i, n)
        {
            cin >> events[i].second;
            if (events[i].second == 0)
                easy++;
        }

        rep(i, n) cin >> events[i].first;

        sort(events.begin(), events.end());

        int ans = 0;
        int count = 0;
        int i = 0;
        int aux = 0;
        for (auto e : events)
        {
            if (e.first - 1 >= count)
                ans = max(ans, i + min((max(0, e.first - 1) - count) / a, easy - aux));

            if (e.second == 0)
            {
                aux++;
                count += a;
            }
            else
                count += b;
            i++;
        }
        if (T >= count)
            ans = n;

        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)

int M, n, T, a, b, easy;
vector<pair<int, int>> events;

int main()
{
    cin >> M;
    rep(m, M)
    {
        cin >> n >> T >> a >> b;

        easy = 0;
        events.resize(n);
        rep(i, n)
        {
            cin >> events[i].second;
            if (events[i].second == 0)
                easy++;
        }

        rep(i, n) cin >> events[i].first;

        sort(events.begin(), events.end());

        int ans = 0;
        int count = 0;
        int i = 0;
        int aux = 0;
        for (auto e : events)
        {
            if (e.first - 1 >= count)
            {
                int extra = e.first - 1 - count;
                int easy_aux = min(extra / a, easy - aux);
                int hard_aux = min((extra - easy_aux * a) / b, n - easy - (i - aux));
                ans = max(ans, i + easy_aux + hard_aux);
            }

            if (e.second == 0)
            {
                aux++;
                count += a;
            }
            else
                count += b;
            i++;
        }
        if (T >= count)
            ans = n;

        cout << ans << '\n';
    }
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,n;
ll len,num,num1,a,T,b,fir,sec,mx,sum;
pair <ll,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		memset(t,0,sizeof(t));
		scanf("%d%I64d%I64d%I64d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%I64d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = num = num1 = 0,mx = -1e9;
		for(int i = 1;i <= n + 1;i++){
			len = t[i].ff - 1 - sum;
			if(len > 0){
				num = min(len / a,fir);
				len -= num * a;
			}
			if(len > 0){
				num1 = min(len / b,sec);
				len -= num1 * b;
			}
			if(t[i].ss) sec--,sum += b;
			else fir--,sum += a;
			if(len >= 0) mx = max(mx,num + num1 + i - 1);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%I64d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,n;
ll len,num,num1,a,T,b,fir,sec,mx,sum;
pair <ll,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		memset(t,0,sizeof(t));
		scanf("%d%I64d%I64d%I64d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%I64d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = num = num1 = 0,mx = -1e9;
		for(int i = 1;i <= n + 1;i++){
			if(i > 1){
				if(t[i - 1].ss) sec--,sum += b;
				else fir--,sum += a;
			}
			len = t[i].ff - 1 - sum;
			if(len < 0) continue;
			if(len >= 0){
				num = min(len / a,fir);
				len -= num * a;
				num1 = min(len / b,sec);
			}
			mx = max(mx,num + num1 + i - 1);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%I64d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,len;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		memset(t,0,sizeof(t));
		scanf("%d%d%d%d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = num = 0,mx = -1e9;
		for(int i = 1;i <= n + 1;i++){
			len = t[i].ff - 1 - sum;
			if(len > 0){
				num = min(len / a,fir);
				len -= min(len / a,fir) * a;
			}
			if(len > 0){
				num += min(len / b,sec);
				len -= min(len / b,sec) * b;
			}
			if(t[i].ss) sec--,sum += b;
			else fir--,sum += a;
			if(len >= 0) mx = max(mx,num + i - 1);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,last;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		memset(t,0,sizeof(t));
		scanf("%d%d%d%d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = num = 0,mx = -1e9;
		for(int i = 0;i <= n;i++){
			if(i){
				if(t[i].ss == 0) fir--,sum += a;
				else sec--,sum += b;
			}
			if(sum >= t[i + 1].ff) continue;
			last = (t[i + 1].ff - 1) - sum;
			if(last > 0) num = min(last / a,fir),last -= min(last / a,fir) * a;
			if(last > 0) num += min(last / b,sec);
			mx = max(num + i,mx);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,last;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		scanf("%d%d%d%d",&n,&T,&a,&b);
		for(int i = 1;i <= n;i++)
			t[i].ff = t[i].ss = 0;
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		n++,t[n] = {T + 1,0};
		sort(t + 1,t + n + 1);
		sum = num = 0,mx = -1e9;
		for(int i = 0;i <= n;i++){
			if(i){
				if(t[i].ss == 0) fir--,sum += a;
				else sec--,sum += b;
			}
			if(sum >= t[i + 1].ff) continue;
			last = (t[i + 1].ff - 1) - sum;
			if(last > 0) num = min(last / a,fir),last -= min(last / a,fir) * a;
			if(last > 0) num += min(last / b,sec);
			mx = max(num + i,mx);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,last;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		scanf("%d%d%d%d",&n,&T,&a,&b);
		for(int i = 1;i <= n;i++)
			t[i][0] = t[i][1] = 0;
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		n++,t[n] = {T + 1,0};
		sort(t + 1,t + n + 1);
		sum = num = 0,mx = -1e9;
		for(int i = 0;i <= n;i++){
			if(i){
				if(t[i].ss == 0) fir--,sum += a;
				else sec--,sum += b;
			}
			if(sum >= t[i + 1].ff) continue;
			last = (t[i + 1].ff - 1) - sum;
			if(last > 0) num = min(last / a,fir),last -= min(last / a,fir) * a;
			if(last > 0) num += min(last / b,sec);
			mx = max(num + i,mx);
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,last,pos;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		scanf("%d%d%d%d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = 0,mx = -1e9,num;
		for(int i = 0;i <= n;i++){
			if(i){
				if(t[i].ss == 0) fir--,sum += a;
				else sec--,sum += b;
			}
			if(sum >= t[i + 1].ff) continue;
			last = (t[i + 1].ff - 1) - sum;
			if(last > 0) num = min(last / a,fir),last -= min(last / a,fir) * a;
			if(last > 0) num += min(last / b,sec);
			if(mx < num + i) mx = num + i,pos = num;
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include <bits/stdc++.h>

#define pb push_back
#define ss second
#define ff first
#define N 200005
#define inf 1000000009
#define ll long long
#define mid(a,b) (a+b)/2
#define pii pair<int,int>

using namespace std;

int ts,sum,num,mx,fir,sec,a,b,T,n,last,pos;
pair <int,int> t[N];

int main()
{
	scanf("%d",&ts);
	while(ts--){
		scanf("%d%d%d%d",&n,&T,&a,&b);
		fir = sec = 0;
		for(int i = 1;i <= n;i++){
			scanf("%d",&t[i].ss);
			if(t[i].ss) sec++;
			else fir++;
		}
		for(int i = 1;i <= n;i++)
			scanf("%d",&t[i].ff);
		sort(t + 1,t + n + 1);
		t[n + 1] = {T + 1,0};
		sum = 0,mx = -1e9,num;
		for(int i = 0;i <= n;i++){
			if(i){
				if(t[i].ss == 0) fir--,sum += a;
				else sec--,sum += b;
			}
			if(sum >= t[i + 1].ff) continue;
			last = t[i + 1].ff - sum;
			if(last > 0) num = min(last / a,fir),last -= min(last / a,fir) * a;
			if(last > 0) num += min(last / b,sec);
			if(mx < num + i) mx = num + i,pos = num;
		}
		if(mx == -1e9) printf("0\n");
		else printf("%d\n",mx);
	}
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N=201000;
const int INF=0x3f3f3f3f;
struct skt{
	int t,kind,rank;
}a[MAX_N];
int b[MAX_N];
int dp[MAX_N];
int x,y;
bool cmp(skt a,skt b){
	int m1,m2;
	m1=a.kind?y:x;
	m2=b.kind?y:x;
	return a.t-m1<b.t-m2;
	//return a.t<b.t;
}
int main(void){
	int n,t,i;
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d%d%d",&n,&t,&x,&y);
		for(i=1;i<=n;i++)
		scanf("%d",&a[i].kind);
		int minl=INF;
		int num=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i].t);
			b[i]=a[i].t;
			if(a[i].kind==1){
				minl=min(minl,a[i].t);
			}
			else
			num++;
		}
		sort(b+1,b+n+1);
		sort(a+1,a+n+1,cmp);
		int s=0;
		int ans=0;
		for(i=1;i<=n;i++){
			if(a[i].kind==0)
			s+=x;
			else
			s+=y;
			if(s>t)
			break;
			if(i+1<=n&&s<b[i+1]){
				//cout<<i<<" "<<s<<" "<<b[i+1]<<"\n";
				ans=i;
			}
		}
		if(s<=t){
			ans=n;
		}
		if(minl!=INF){
			ans=max(ans,min(num,(minl-1)/x));
		}
		printf("%d\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAX_N=201000;
const int INF=0x3f3f3f3f;
struct skt{
	int t,kind,rank;
}a[MAX_N];
int b[MAX_N];
int dp[MAX_N];
int x,y;
bool cmp(skt a,skt b){
	int m1,m2;
	m1=a.kind?y:x;
	m2=b.kind?y:x;
	return a.t-m1<b.t-m2;
	//return a.t<b.t;
}
int main(void){
	int n,t,i;
	int T;
	cin>>T;
	while(T--){
		scanf("%d%d%d%d",&n,&t,&x,&y);
		for(i=1;i<=n;i++)
		scanf("%d",&a[i].kind);
		int minl=INF;
		int num=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i].t);
			b[i]=a[i].t;
			if(a[i].kind==1){
				minl=min(minl,a[i].t);
			}
			else
			num++;
		}
		sort(b+1,b+n+1);
		sort(a+1,a+n+1,cmp);
		int s=0;
		int ans=0;
		for(i=1;i<=n;i++){
			if(a[i].kind==0)
			s+=x;
			else
			s+=y;
			if(s>t)
			break;
			if(i+1<=n&&s<b[i+1]){
				//cout<<i<<" "<<s<<" "<<b[i+1]<<"\n";
				ans=i;
			}
		}
		if(s<=t){
			ans=n;
		}
		if(minl!=INF){
			ans=max(ans,min(num,(minl-1)/x));
		}
		printf("%d!\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=10100;
struct skt{
	long long t;
	int kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	int n,T,i,j;
	long long t,x,y;
	scanf("%d",&T);
	while(T--){
		scanf("%d%lld%lld%lld",&n,&t,&x,&y);
		int num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%lld",&a[i].t);
		sort(a+1,a+n+1,cmp);
		int ans=0;
		for(i=1;i<=n;i++){
			if(st>t)
			break;
			if(!a[i].kind){
				if(st+x<a[i+1].t){
					ans=i;
				}
				num--;
				st+=x;
			}
			else{
				if(st+y<a[i+1].t){
					ans=max(ans,i);
				}
				else if(st<a[i].t){
					int k=(int)(a[i].t-1-st)/x;
					k=min(k,num);
					ans=max(ans,i-1+k);
				}
				st+=y;
			}
		}
		if(st<=t)
		ans=n;
		printf("%d\n",ans);
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=200100;
struct skt{
	long long t,kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	long long n,T,i,j,res;
	long long t,x,y;
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld%lld",&n,&t,&x,&y);
		long long num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%lld",&a[i].t);
		if(x*num+y*(n-num)<=t){
			printf("%lld\n",n);
			continue;
		}
		sort(a+1,a+n+1,cmp);
		long long ans=0;
		//cout<<a[1].t<<"！hi！！！\n";
		if(a[1].t)
		ans=min((a[1].t-1)/x,a[1].kind?num:num-1);
		a[n+1].t=t+1;
		for(i=1;i<=n;i++){
			res=0;
			if(!a[i].kind){
				num--;
				st+=x;
			}
			else
			st+=y;
			if(st<a[i+1].t){
				res=i;
				//cout<<res<<" hi\n";
				res+=min((a[i+1].t-1-st)/x,num);
			}
			//cout<<res<<" res\n";
			ans=max(ans,res);
			//cout<<ans<<"\n";
		}
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=10100;
struct skt{
	long long t,kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	long long n,T,i,j;
	long long t,x,y;
	scanf("%lld",&T);
	while(T--){
		scanf("%lld%lld%lld%lld",&n,&t,&x,&y);
		long long num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%lld",&a[i].t);
		if(x*num+y*(n-num)<=t){
			printf("%lld\n",n);
			continue;
		}
		sort(a+1,a+n+1,cmp);
		long long ans=0;
		a[n+1].t=t+1;
		for(i=1;i<=n;i++){
			if(st>t)
			break;
			if(!a[i].kind){
				if(st+x<a[i+1].t){
					ans=max(ans,i);
				}
				num--;
				st+=x;
			}
			else{
				if(st+y<a[i+1].t){
					ans=max(ans,i);
				}
				else if(st<a[i].t){
					long long k=(a[i].t-1-st)/x;
					k=min(k,num);
					ans=max(ans,i-1+k);
				}
				st+=y;
			}
		}
		if(st<=t)
		ans=n;
		printf("%lld\n",ans);
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=10100;
struct skt{
	long long t,kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	long long n,T,i,j;
	long long t,x,y;
	scanf("%d",&T);
	while(T--){
		scanf("%lld%lld%lld%lld",&n,&t,&x,&y);
		long long num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%lld",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%lld",&a[i].t);
		sort(a+1,a+n+1,cmp);
		int ans=0;
		a[n+1].t=t;
		for(i=1;i<=n;i++){
			if(st>t)
			break;
			if(!a[i].kind){
				if(st+x<a[i+1].t){
					ans=max(ans,i);
				}
				num--;
				st+=x;
			}
			else{
				if(st+y<a[i+1].t){
					ans=max(ans,i);
				}
				else if(st<a[i].t){
					long long k=(a[i].t-1-st)/x;
					k=min(k,num);
					ans=max(ans,i-1+k);
				}
				st+=y;
			}
		}
		if(st<=t)
		ans=n;
		printf("%lld\n",ans);
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=10100;
struct skt{
	int t,kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	int n,T,i,j;
	long long t,x,y;
	scanf("%d",&T);
	while(T--){
		scanf("%d%lld%lld%lld",&n,&t,&x,&y);
		int num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%d",&a[i].t);
		sort(a+1,a+n+1,cmp);
		int ans=0;
		for(i=1;i<=n;i++){
			if(st>t)
			break;
			if(!a[i].kind){
				if(st+x<a[i+1].t){
					ans=i;
				}
				num--;
				st+=x;
			}
			else{
				if(st+y<a[i+1].t){
					ans=max(ans,i);
				}
				else if(st<a[i].t){
					int k=(int)(a[i].t-1-st)/x;
					k=min(k,num);
					ans=max(ans,i-1+k);
				}
				st+=y;
			}
		}
		if(st<=t)
		ans=n;
		printf("%d\n",ans);
	}
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX_N=10100;
struct skt{
	long long t;
	int kind;
}a[MAX_N];
bool cmp(skt a,skt b){
	return a.t<b.t;
}
int main(void){
	int n,T,i,j;
	long long t,x,y;
	scanf("%d",&T);
	while(T--){
		scanf("%d%lld%lld%lld",&n,&t,&x,&y);
		int num=0;
		long long st=0;
		for(i=1;i<=n;i++){
			scanf("%d",&a[i].kind);
			if(!a[i].kind)
			num++;
		}
		for(i=1;i<=n;i++)
		scanf("%lld",&a[i].t);
		sort(a+1,a+n+1,cmp);
		int ans=0;
		a[n+1].t=t;
		for(i=1;i<=n;i++){
			if(st>t)
			break;
			if(!a[i].kind){
				if(st+x<a[i+1].t){
					ans=max(ans,i);
				}
				num--;
				st+=x;
			}
			else{
				if(st+y<a[i+1].t){
					ans=max(ans,i);
				}
				else if(st<a[i].t){
					long long k=(a[i].t-1-st)/x;
					k=min(k,(long long)num);
					ans=max(ans,i-1+(int)k);
				}
				st+=y;
			}
		}
		if(st<=t)
		ans=n;
		printf("%d\n",ans);
	}
}