#include <bits/stdc++.h>

using namespace std;

#define N 200005
#define ll long long
#define fi first
#define se second
#define pii pair<int , int>
#define pb push_back
#define int ll

pii arr[N];
int cnt_a[N] , cnt_b[N];
int sum[N];

 main()
{
    ios_base :: sync_with_stdio(false);
    int testcase;
    cin >> testcase;
    int n , T , a , b;
    int ans , temp;
    int rem;
    while(testcase--){
        cin >> n >> T >> a >> b;
        ans = 0;
        for(int i = 0 ; i < n ; i++)
            cin >> arr[i].se;

        for(int i = 0 ; i < n ; i++)
            cin >> arr[i].fi;

        sort(arr , arr + n);

        for(int i = 0 ; i <= n ; i++){
            cnt_a[i] = cnt_b[i] = 0;
            sum[i] = 0;
        }

        for(int i = n - 2 ; i >= 0 ; i--){
            if(arr[i + 1].se == 0)
                cnt_a[i] ++;
            else
                cnt_b[i] ++;

            cnt_a[i] += cnt_a[i + 1];
            cnt_b[i] += cnt_b[i + 1];
        }

        for(int i = 0 ; i < n ; i++){
            if(arr[i].se == 0)
                sum[i] = a;
            else
                sum[i] = b;
            if(i != 0)
                sum[i] += sum[i - 1];
        }

        arr[n].fi = sum[n - 1] + 1;

        for(int i = 0 ; i < n ; i++){

            if(sum[i] >= arr[i + 1].fi)
                continue;

            if(sum[i] > T)
                break;

            if(i != n - 1)
                rem = arr[i + 1].fi - sum[i] - 1;
            else{
                rem = T - sum[i];
            }

            temp = i + 1;
            temp += min(rem / a , cnt_a[i]);
            rem -= (min(rem / a , cnt_a[i]) * a);
            temp += min(rem / b , cnt_b[i]);
            ans = max(ans , temp);
        }

        temp = 0;
        rem = arr[0].fi - 1;
        temp += (min(rem / a , cnt_a[0]));
        rem -= min(rem / a , cnt_a[0]) * a;
        temp += min(rem / b , cnt_b[0]);
        ans = max(ans , temp);
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 200005
#define ll long long

pii arr[N];
int num_a[N];
int num_b[N];

int main(){
    ios_base :: sync_with_stdio(false);
    int testcase;
    cin >> testcase;
    int n , t , a , b;
    while(testcase--){
        cin >> n >> t >> a >> b;
        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].se;
            if(arr[i].se == 0)
                arr[i].se = a;
            else
                arr[i].se = b;
        }

        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].fi;
            num_a[i] = num_b[i] = 0;
        }
        num_a[n] = num_b[n] = 0;

        sort(arr , arr + n);

        for(int i = n - 1 ; i >= 0 ; i--){
            if(arr[i].se == a){
                num_a[i] = 1;
                if(i != n - 1)
                    num_a[i] += num_a[i + 1];
            }

            else{
                num_b[i] = 1;
                if(i != n - 1)
                    num_b[i] += num_b[i + 1];
            }
        }
        int sum = 0;
        int ans = 0;
        int num;
        int temp , x;
        for(int i = 0 ; i < n ; i++){
            sum += arr[i].se;

            if(sum > t)
                break;
            if(i != n - 1 && arr[i + 1].fi <= sum)
                continue;

            num = i + 1;
            if(i != n - 1){
                temp = arr[i + 1].fi - sum;
                x = temp / a;
                num += min(x , num_a[i + 1]);
                temp -= min(x , num_a[i + 1]) * a;
                x = temp / b;
                num += min(x , num_b[i + 1]);
            }

            ans = max(ans , num);
        }
        num = 0;
        temp = arr[0].fi;
        x = temp / a;
        num += min(x , num_a[1]);
        temp -= min(x , num_a[1]);
        x = temp / b;
        num += min(x , num_b[1]);
        ans = max(ans , num);
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 200005
#define ll long long

pii arr[N];
int num_a[N];
int num_b[N];

int main(){
    ios_base :: sync_with_stdio(false);
    int testcase;
    cin >> testcase;
    int n , t , a , b;
    while(testcase--){
        cin >> n >> t >> a >> b;
        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].se;
            if(arr[i].se == 0)
                arr[i].se = a;
            else
                arr[i].se = b;
        }

        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].fi;
            num_a[i] = num_b[i] = 0;
        }
        num_a[n] = num_b[n] = 0;

        sort(arr , arr + n);

        for(int i = n - 1 ; i >= 0 ; i--){
            if(arr[i].se == a){
                num_a[i] = num_a[i + 1] + 1;
                num_b[i] = num_b[i + 1];
            }

            else{
                num_b[i] = 1 + num_b[i + 1];
                num_a[i] = num_a[i + 1];
            }
        }
        int sum = 0;
        int ans = 0;
        int num;
        int temp , x;
        for(int i = 0 ; i < n ; i++){
            sum += arr[i].se;

            if(sum > t)
                break;
            if(i != n - 1 && arr[i + 1].fi <= sum)
                continue;

            num = i + 1;
            if(i != n - 1){
                temp = arr[i + 1].fi - sum - 1;
                x = temp / a;
                num += min(x , num_a[i + 1]);
                temp -= min(x , num_a[i + 1]) * a;
                x = temp / b;
                num += min(x , num_b[i + 1]);
            }

            ans = max(ans , num);
        }

        num = 0;
        temp = arr[0].fi - 1;
        x = temp / a;
        num += min(x , num_a[1]);
        temp -= min(x , num_a[1]) * a;
        x = temp / b;
        num += min(x , num_b[1]);
        ans = max(ans , num);
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define N 200005
#define ll long long
#define fi first
#define se second
#define pii pair<int , int>
#define pb push_back

pii arr[N];
int cnt_a[N] , cnt_b[N];
int sum[N];

int main()
{
    ios_base :: sync_with_stdio(false);
    int testcase;
    cin >> testcase;
    int n , T , a , b;
    int ans , temp;
    int rem;
    while(testcase--){
        cin >> n >> T >> a >> b;
        ans = 0;
        for(int i = 0 ; i < n ; i++)
            cin >> arr[i].se;

        for(int i = 0 ; i < n ; i++)
            cin >> arr[i].fi;

        sort(arr , arr + n);

        for(int i = 0 ; i <= n ; i++){
            cnt_a[i] = cnt_b[i] = 0;
            sum[i] = 0;
        }

        for(int i = n - 2 ; i >= 0 ; i--){
            if(arr[i + 1].se == 0)
                cnt_a[i] ++;
            else
                cnt_b[i] ++;

            cnt_a[i] += cnt_a[i + 1];
            cnt_b[i] += cnt_b[i + 1];
        }

        for(int i = 0 ; i < n ; i++){
            if(arr[i].se == 0)
                sum[i] = a;
            else
                sum[i] = b;
            if(i != 0)
                sum[i] += sum[i - 1];
        }

        arr[n].fi = sum[n - 1] + 1;

        for(int i = 0 ; i < n ; i++){

            if(sum[i] >= arr[i + 1].fi)
                continue;

            if(sum[i] > T)
                break;

            rem = min(T , arr[i + 1].fi) - sum[i];

            temp = i + 1;
            temp += min(rem / a , cnt_a[i]);
            rem -= (min(rem / a , cnt_a[i]) * a);
            temp += min(rem / b , cnt_b[i]);

            ans = max(ans , temp);
        }

        temp = 0;
        rem = arr[0].fi - 1;
        temp += (min(rem / a , cnt_a[0]));
        rem -= min(rem / a , cnt_a[0]) * a;
        temp += min(rem / b , cnt_b[0]);
        ans = max(ans , temp);
        cout << ans << endl;
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define pii pair<int, long long>
#define pb push_back
#define fi first
#define se second
#define N 200005
#define ll long long

pii arr[N];
int num_a[N];
int num_b[N];

int main(){
    ios_base :: sync_with_stdio(false);
    int testcase;
    cin >> testcase;
    int n , t , a , b;
    while(testcase--){
        cin >> n >> t >> a >> b;
        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].se;
            if(arr[i].se == 0)
                arr[i].se = a;
            else
                arr[i].se = b;
        }

        for(int i = 0 ; i < n ; i++){
            cin >> arr[i].fi;
            num_a[i] = num_b[i] = 0;
        }
        num_a[n] = num_b[n] = 0;

        sort(arr , arr + n);

        for(int i = n - 1 ; i >= 0 ; i--){
            if(arr[i].se == a){
                num_a[i] = num_a[i + 1] + 1;
                num_b[i] = num_b[i + 1];
            }

            else{
                num_b[i] = 1 + num_b[i + 1];
                num_a[i] = num_a[i + 1];
            }
        }
        int sum = 0;
        int ans = 0;
        int num;
        int temp , x;
        for(int i = 0 ; i < n ; i++){
            sum += arr[i].se;

            if(sum > t)
                break;
            if(i != n - 1 && arr[i + 1].fi <= sum)
                continue;

            num = i + 1;
            if(i != n - 1){
                temp = arr[i + 1].fi - sum;
                x = temp / a;
                num += min(x , num_a[i + 1]);
                temp -= min(x , num_a[i + 1]) * a;
                x = temp / b;
                num += min(x , num_b[i + 1]);
            }

            ans = max(ans , num);
        }

        num = 0;
        temp = arr[0].fi;
        x = temp / a;
        num += min(x , num_a[1]);
        temp -= min(x , num_a[1]) * a;
        x = temp / b;
        num += min(x , num_b[1]);
        ans = max(ans , num);
        cout << ans << endl;
    }
    return 0;
}


using namespace std;
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define repa(i,a,b) for (int i=a; i<=b; i++)
#define repd(i,b,a) for (int i=b; i>=a; i--)
#define endl "\n"
//debugging
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define show(x) cout<<#x<<" = "<<(x)<<" "
#define ENA cout<<endl;
#define ENAA cout<<endl<<endl;

int MAX = 1e+5+5;
int BIG = 1e+9;
int MOD = 1e+9+7;

signed main()
{
    IOS;
    int t; cin>>t; while(t--)
    {
        int n,T,a,b; cin>>n>>T>>a>>b;

        int diff[n+1];

        repa(i,1,n) cin>>diff[i];

        vector< pair<int,int> > v(n+1);

        map<int,int> easy,hard;
        int totEasy = 0;
        int totHard = 0;
        int prevEasy = 0;
        int prevHard = 0;
        int prevTime = 0;
        int maxPoints = 0;

        repa(i,1,n)
        {
            cin>>v[i].ff;
            v[i].ss=i;
            easy[v[i].ff]+=(diff[i]==0);
            hard[v[i].ff]+=(diff[i]==1);
            totHard+=(diff[i]==1);
            totEasy+=(diff[i]==0);
        }

        for (auto it: easy)
        {
            int val = it.ff;
            //debug(val); debug(prevEasy); debug(prevHard);
            if (prevHard*b+prevEasy*a<val)
            {

                int rem = val;

                rem-=prevHard*b+prevEasy*a;
                rem--;

                int remEasy = totEasy - prevEasy;
                int remHard = totHard - prevHard;
                int bufAns = prevHard + prevEasy;
                int tempAns = 0;

                tempAns+=min(remEasy, rem/a );
                rem-=tempAns*a;

                tempAns+=min(remHard, rem/b );

                maxPoints = max(maxPoints,bufAns+tempAns);

            }

            prevHard+=hard[val];
            prevEasy+=easy[val];
        }
        if (prevHard*b+prevEasy*a<=T)
            maxPoints = prevHard + prevEasy;

        cout<<max(maxPoints,(int)0)<<endl;

    }

}


using namespace std;
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define repa(i,a,b) for (int i=a; i<=b; i++)
#define repd(i,b,a) for (int i=b; i>=a; i--)
#define endl "\n"
//debugging
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define show(x) cout<<#x<<" = "<<(x)<<" "
#define ENA cout<<endl;
#define ENAA cout<<endl<<endl;

int MAX = 1e+5+5;
int BIG = 1e+9;
int MOD = 1e+9+7;

signed main()
{
    IOS;
    int t; cin>>t; while(t--)
    {
        int n,T,a,b; cin>>n>>T>>a>>b;

        int diff[n+1];

        repa(i,1,n) cin>>diff[i];

        vector< pair<int,int> > v(n+1);

        map<int,int> easy,hard;
        int totEasy = 0;
        int totHard = 0;
        int prevEasy = 0;
        int prevHard = 0;
        int prevTime = 0;
        int maxPoints = 0;

        repa(i,1,n)
        {
            cin>>v[i].ff;
            v[i].ss=i;
            easy[v[i].ff]+=(diff[i]==0);
            hard[v[i].ff]+=(diff[i]==1);
            totHard+=(diff[i]==1);
            totEasy+=(diff[i]==0);
        }

        for (auto it: easy)
        {
            int val = it.ff;
            //debug(val); debug(prevEasy); debug(prevHard);
            if (prevHard*b+prevEasy*a<val)
            {
                int rem = val;
                rem-=prevHard*b+prevEasy*a;
                int remEasy = totEasy - prevEasy;
                int remHard = totHard - prevHard;
                int bufAns = prevHard + prevEasy;
                bufAns+=min(remEasy, (rem/a-(rem%a==0)) );
                rem-=bufAns*a;
                bufAns+=min(remHard, (rem/b-(rem%b==0)) );
                maxPoints = max(maxPoints,bufAns);
            }

            prevHard+=hard[val];
            prevEasy+=easy[val];
        }
        if (prevHard*b+prevEasy*a<=T)
            maxPoints = prevHard + prevEasy;

        int bufAns = 0;
        int minV = easy.begin()->ff;
        bufAns+=min(prevEasy, minV/a-(minV%a==0));
        minV-=bufAns*a;
        bufAns+=min(prevHard, minV/b-(minV%b==0));

        cout<<max(maxPoints,bufAns)<<endl;

    }

}


using namespace std;
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define repa(i,a,b) for (int i=a; i<=b; i++)
#define repd(i,b,a) for (int i=b; i>=a; i--)
#define endl "\n"
//debugging
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define show(x) cout<<#x<<" = "<<(x)<<" "
#define ENA cout<<endl;
#define ENAA cout<<endl<<endl;

int MAX = 1e+5+5;
int BIG = 1e+9;
int MOD = 1e+9+7;

signed main()
{
    IOS;
    int t; cin>>t; while(t--)
    {
        int n,T,a,b; cin>>n>>T>>a>>b;

        int diff[n+1];

        repa(i,1,n) cin>>diff[i];

        vector< pair<int,int> > v(n+1);

        map<int,int> easy,hard;
        int totEasy = 0;
        int totHard = 0;
        int prevEasy = 0;
        int prevHard = 0;
        int prevTime = 0;
        int maxPoints = 0;

        repa(i,1,n)
        {
            cin>>v[i].ff;
            v[i].ss=i;
            easy[v[i].ff]+=(diff[i]==0);
            hard[v[i].ff]+=(diff[i]==1);
            totHard+=(diff[i]==1);
            totEasy+=(diff[i]==0);
        }

        for (auto it: easy)
        {
            int val = it.ff;
            //debug(val); debug(prevEasy); debug(prevHard);
            if (prevHard*b+prevEasy*a<val)
            {
                int rem = val;
                rem-=prevHard*b+prevEasy*a;
                int remEasy = totEasy - prevEasy;
                int remHard = totHard - prevHard;
                int bufAns = prevHard + prevEasy;
              /*  bufAns+=min(remEasy, rem/a-(rem%a==0));
                rem-=bufAns*a;
                bufAns+=min(remHard, rem/b-(rem%b==0));*/
                maxPoints = max(maxPoints,bufAns);
            }

            prevHard+=hard[val];
            prevEasy+=easy[val];
        }
        if (prevHard*b+prevEasy*a<=T)
            maxPoints = prevHard + prevEasy;

        int bufAns = 0;
        int minV = easy.begin()->ff;
        bufAns+=min(prevEasy, minV/a-(minV%a==0));
        minV-=bufAns*a;
        bufAns+=min(prevHard, minV/b-(minV%b==0));

        cout<<max(maxPoints,bufAns)<<endl;

    }

}


using namespace std;
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define repa(i,a,b) for (int i=a; i<=b; i++)
#define repd(i,b,a) for (int i=b; i>=a; i--)
#define endl "\n"
//debugging
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define show(x) cout<<#x<<" = "<<(x)<<" "
#define ENA cout<<endl;
#define ENAA cout<<endl<<endl;

int MAX = 1e+5+5;
int BIG = 1e+9;
int MOD = 1e+9+7;

signed main()
{
    IOS;
    int t; cin>>t; while(t--)
    {
        int n,T,a,b; cin>>n>>T>>a>>b;

        int diff[n+1];

        repa(i,1,n) cin>>diff[i];

        vector< pair<int,int> > v(n+1);

        map<int,int> easy,hard;
        int totEasy = 0;
        int totHard = 0;
        int prevEasy = 0;
        int prevHard = 0;
        int prevTime = 0;
        int maxPoints = 0;

        repa(i,1,n)
        {
            cin>>v[i].ff;
            v[i].ss=i;
            easy[v[i].ff]+=(diff[i]==0);
            hard[v[i].ff]+=(diff[i]==1);
            totHard+=(diff[i]==1);
            totEasy+=(diff[i]==0);
        }

        for (auto it: easy)
        {
            int val = it.ff;
            //debug(val); debug(prevEasy); debug(prevHard);
            if (prevHard*b+prevEasy*a<val)
            {
                int rem = val;
                rem-=prevHard*b+prevEasy*a;
                int remEasy = totEasy - prevEasy;
                int remHard = totHard - prevHard;
                int bufAns = prevHard + prevEasy;
                bufAns+=min(remEasy, rem/a-(rem%a==0));
                rem-=bufAns*a;
                bufAns+=min(remHard, rem/b-(rem%b==0));
                maxPoints = max(maxPoints,bufAns);
            }

            prevHard+=hard[val];
            prevEasy+=easy[val];
        }
        if (prevHard*b+prevEasy*a<=T)
            maxPoints = prevHard + prevEasy;

        int bufAns = 0;
        int minV = easy.begin()->ff;
        bufAns+=min(prevEasy, minV/a-(minV%a==0));
        minV-=bufAns*a;
        bufAns+=min(prevHard, minV/b-(minV%b==0));

        cout<<max(maxPoints,bufAns)<<endl;

    }

}


using namespace std;
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define ff first
#define ss second
#define repa(i,a,b) for (int i=a; i<=b; i++)
#define repd(i,b,a) for (int i=b; i>=a; i--)
#define endl "\n"
//debugging
#define debug(x) cout<<#x<<" = "<<(x)<<endl
#define show(x) cout<<#x<<" = "<<(x)<<" "
#define ENA cout<<endl;
#define ENAA cout<<endl<<endl;

int MAX = 1e+5+5;
int BIG = 1e+9;
int MOD = 1e+9+7;

signed main()
{
    IOS;
    int t; cin>>t; while(t--)
    {
        int n,T,a,b; cin>>n>>T>>a>>b;

        int diff[n+1];

        repa(i,1,n) cin>>diff[i];

        vector< pair<int,int> > v(n+1);

        map<int,int> easy,hard;
        int totEasy = 0;
        int totHard = 0;
        int prevEasy = 0;
        int prevHard = 0;
        int prevTime = 0;
        int maxPoints = 0;

        repa(i,1,n)
        {
            cin>>v[i].ff;
            v[i].ss=i;
            easy[v[i].ff]+=(diff[i]==0);
            hard[v[i].ff]+=(diff[i]==1);
            totHard+=(diff[i]==1);
            totEasy+=(diff[i]==0);
        }

        for (auto it: easy)
        {
            int val = it.ff;
            //debug(val); debug(prevEasy); debug(prevHard);
            if (prevHard*b+prevEasy*a<val)
            {
                int rem = val;
                rem-=prevHard*b+prevEasy*a;
                int remEasy = totEasy - prevEasy;
                int remHard = totHard - prevHard;
                int bufAns = prevHard + prevEasy;
                bufAns+=min(remEasy, ( (rem/a)-(rem%a==0)) );
                rem-=bufAns*a;
                bufAns+=min(remHard, ( (rem/b)-(rem%b==0)) );
                maxPoints = max(maxPoints,bufAns);
            }

            prevHard+=hard[val];
            prevEasy+=easy[val];
        }
        if (prevHard*b+prevEasy*a<=T)
            maxPoints = prevHard + prevEasy;

        int bufAns = 0;
        int minV = easy.begin()->ff;
        bufAns+=min(prevEasy, minV/a-(minV%a==0));
        minV-=bufAns*a;
        bufAns+=min(prevHard, minV/b-(minV%b==0));

        cout<<max(maxPoints,bufAns)<<endl;

    }

}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int main() {_
    ll m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        ll x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        ll contD = 0;
        ll contF = 0;
        for(ll I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                contD++;
            } else {
                contF++;
            }
        }
        for(ll I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        sort(tempo.begin(), tempo.end());
        tempo.pb({t+1, -1});
        vector<ii> necessTemp(n);
        for(ll I=0; I<n; I++) {
            if(I != 0) {
                necessTemp[I] = necessTemp[I-1];
            }
            if(ehDificil[tempo[I].s]) {
                necessTemp[I].s++;
            } else {
                necessTemp[I].f++;
            }
        }
        ll antes = tempo[0].f-1;
        ll nF = 0;
        ll nD = 0;
        if(antes > 0) {
            nF = min(antes/a, contF);
            antes -= nF*a;
            antes = max(0, antes);
            nD = min(antes/b, contD);
        }
        ll resp = nF+nD;
        for(ll I=0; I<n; I++) {
            ll tempoGasto = necessTemp[I].f*a + necessTemp[I].s*b;
            if(tempoGasto < tempo[I+1].f) {
                ll tmp = tempo[I+1].f-1-tempoGasto;
                ll nF = min(tmp/a, contF-necessTemp[I].f);
                tmp-= nF*a;
                ll nD = min(tmp/b, contD-necessTemp[I].s);
                resp = max(resp, nF+nD+necessTemp[I].f+necessTemp[I].s);
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<long long, long long> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int main() {_
    ll m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        ll x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        ll contD = 0;
        ll contF = 0;
        for(ll I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                contD++;
            } else {
                contF++;
            }
        }
        for(ll I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        sort(tempo.begin(), tempo.end());
        tempo.pb({t+1, -1});
        vector<ii> necessTemp(n);
        for(ll I=0; I<n; I++) {
            if(I != 0) {
                necessTemp[I] = necessTemp[I-1];
            }
            if(ehDificil[tempo[I].s]) {
                necessTemp[I].s++;
            } else {
                necessTemp[I].f++;
            }
        }
        ll antes = tempo[0].f-1;
        ll nF = 0;
        ll nD = 0;
        if(antes > 0) {
            nF = min(antes/a, contF);
            antes -= nF*a;
            antes = max(0LL, antes);
            nD = min(antes/b, contD);
        }
        ll resp = nF+nD;
        for(ll I=0; I<n; I++) {
            ll tempoGasto = necessTemp[I].f*a + necessTemp[I].s*b;
            if(tempoGasto < tempo[I+1].f) {
                ll tmp = tempo[I+1].f-1-tempoGasto;
                ll nF = min(tmp/a, contF-necessTemp[I].f);
                tmp-= nF*a;
                ll nD = min(tmp/b, contD-necessTemp[I].s);
                resp = max(resp, nF+nD+necessTemp[I].f+necessTemp[I].s);
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int tempoQuest(int x) {
    if(ehDificil[x]) {
        return b;
    } else {
        return a;
    }
}

int main() {_
    int m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        int x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        set<int> restDificil;
        set<int> restFacil;
        for(int I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                restDificil.insert(I);
            } else {
                restFacil.insert(I);
            }
        }
        for(int I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        tempo.pb({0, -1});
        sort(tempo.begin(), tempo.end());
        tempo.pb({t, -1});
        int resp = 0;
        set<int> feitas;
        int sobrando = 0;
        for(int I=1; I<=n+1; I++) {
            int proxTempo = tempo[I].f-tempo[I-1].f;
            int tempoEntre = proxTempo + sobrando;
            if(tempoEntre > 0) {
                resp = feitas.size();
            }
            if(tempo[I].s != -1 && feitas.find(tempo[I].s) == feitas.end()) {
                feitas.insert(tempo[I].s);
                if(ehDificil[tempo[I].s]) {
                    restDificil.erase(tempo[I].s);
                } else {
                    restFacil.erase(tempo[I].s);
                }
                tempoEntre-=tempoQuest(tempo[I].s);
            }
            if(tempoEntre >= 0) {
                resp = feitas.size();
                while((tempoEntre-b >= 0 || tempoEntre-a >= 0) && feitas.size() < n) {
                    if(tempoEntre-b >= 0 && !restDificil.empty()) {
                        tempoEntre-=b;
                        feitas.insert(*restDificil.begin());
                        restDificil.erase(restDificil.begin());
                    } else {
                        tempoEntre-=a;
                        if(restFacil.empty()) {
                            break;
                        }
                        feitas.insert(*restFacil.begin());
                        restFacil.erase(restFacil.begin());
                    }
                }
            }
            sobrando = tempoEntre;
        }
        if(resp == 0) {
            if(tempo[1].f > a) {
                int vezes = tempo[1].f/a;
                cout << vezes << endl;
                continue;
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int main() {_
    int m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        int x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        set<int> restDificil;
        set<int> restFacil;
        int contD = 0;
        int contF = 0;
        for(int I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                contD++;
            } else {
                contF++;
            }
        }
        for(int I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        sort(tempo.begin(), tempo.end());
        tempo.pb({t+1, -1});
        vector<ii> necessTemp(n);
        for(int I=0; I<n; I++) {
            if(I != 0) {
                necessTemp[I] = necessTemp[I-1];
            }
            if(ehDificil[tempo[I].s]) {
                necessTemp[I].s++;
            } else {
                necessTemp[I].f++;
            }
        }
        int antes = tempo[0].f-1;
        int nF = 0;
        int nD = 0;
        if(antes > 0) {
            nF = min(antes/a, contF);
            antes -= nF*a;
            antes = max(0, antes);
            nD = min(antes/b, contD);
        }
        int resp = nF+nD;
        for(int I=0; I<n; I++) {
            int tempoGasto = necessTemp[I].f*a + necessTemp[I].s*b;
            if(tempoGasto < tempo[I+1].f) {
                int tmp = tempo[I+1].f-1-tempoGasto;
                int nF = min(tmp/a, contF-necessTemp[I].f);
                tmp-= nF*a;
                int nD = min(tmp/b, contD-necessTemp[I].s);
                resp = max(resp, nF+nD+necessTemp[I].f+necessTemp[I].s);
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int main() {_
    ll m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        ll x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        ll contD = 0;
        ll contF = 0;
        for(ll I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                contD++;
            } else {
                contF++;
            }
        }
        for(ll I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        sort(tempo.begin(), tempo.end());
        tempo.pb({t+1, -1});
        vector<ii> necessTemp(n);
        for(ll I=0; I<n; I++) {
            if(I != 0) {
                necessTemp[I] = necessTemp[I-1];
            }
            if(ehDificil[tempo[I].s]) {
                necessTemp[I].s++;
            } else {
                necessTemp[I].f++;
            }
        }
        ll antes = tempo[0].f-1;
        ll nF = 0;
        ll nD = 0;
        if(antes > 0) {
            nF = min(antes/a, contF);
            antes -= nF*a;
            antes = max(0LL, antes);
            nD = min(antes/b, contD);
        }
        ll resp = nF+nD;
        for(ll I=0; I<n; I++) {
            ll tempoGasto = necessTemp[I].f*a + necessTemp[I].s*b;
            if(tempoGasto < tempo[I+1].f) {
                ll tmp = tempo[I+1].f-1-tempoGasto;
                ll nF = min(tmp/a, contF-necessTemp[I].f);
                tmp-= nF*a;
                ll nD = min(tmp/b, contD-necessTemp[I].s);
                resp = max(resp, nF+nD+necessTemp[I].f+necessTemp[I].s);
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

/*
    VERIFICAR:
    - Corner cases
    - Estouro de variavel
    - Acesso indevido de memoria

    MODULO NEGATIVO
    int modulo(int x,int N){
        return (x % N + N) %N;
    }

*/

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int n, t, a, b;
vector<ii> tempo;
vector<bool> ehDificil;

int tempoQuest(int x) {
    if(ehDificil[x]) {
        return b;
    } else {
        return a;
    }
}

int main() {_
    int m;
    cin >> m;
    while(m--) {
        cin >> n >> t >> a >> b;
        int x;
        tempo.clear();
        ehDificil.clear();
        tempo.resize(n);
        ehDificil.resize(n);
        set<int> restDificil;
        set<int> restFacil;
        for(int I=0; I<n; I++) {
            cin >> x;
            if(x == 1) {
                ehDificil[I] = true;
                restDificil.insert(I);
            } else {
                restFacil.insert(I);
            }
        }
        for(int I=0; I<n; I++) {
            cin >> tempo[I].f;
            tempo[I].s = I;
        }
        tempo.pb({0, -1});
        sort(tempo.begin(), tempo.end());
        tempo.pb({t, -1});
        int resp = 0;
        set<int> feitas;
        int sobrando = 0;
        for(int I=1; I<=n+1; I++) {
            int proxTempo = tempo[I].f-tempo[I-1].f;
            int tempoEntre = proxTempo + sobrando;
            if(tempoEntre > 0) {
                resp = feitas.size();
            }
            if(tempo[I].s != -1 && feitas.find(tempo[I].s) == feitas.end()) {
                feitas.insert(tempo[I].s);
                if(ehDificil[tempo[I].s]) {
                    restDificil.erase(tempo[I].s);
                } else {
                    restFacil.erase(tempo[I].s);
                }
                tempoEntre-=tempoQuest(tempo[I].s);
            }
            if(tempoEntre >= 0) {
                resp = feitas.size();
                while((tempoEntre-b >= 0 || tempoEntre-a >= 0) && feitas.size() < n) {
                    if(tempoEntre-a >= 0 && !restFacil.empty()) {
                        tempoEntre-=a;
                        feitas.insert(*restFacil.begin());
                        restFacil.erase(restFacil.begin());
                    } else {
                        tempoEntre-=b;
                        if(restDificil.empty()) {
                            break;
                        }
                        feitas.insert(*restDificil.begin());
                        restDificil.erase(restDificil.begin());
                    }
                }
            }
            sobrando = tempoEntre;
        }
        if(resp == 0) {
            if(tempo[1].f > a) {
                int vezes = tempo[1].f/a;
                cout << vezes << endl;
                continue;
            }
        }
        cout << resp << endl;
    }
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2],ttt;
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        ttt.clear();
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
            ttt.push_back(x);
        }
        if(ca == 445){
            for(int i = 0;i < n;i++)printf("%d77",ttt[i]);
            return 0;
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        int ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = i+j+k+l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2];
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    while(T--){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        int ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = i+j+k+l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2];
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        ll ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = max(ans,i+j+k+l);
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2];
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        if(ca == 445){
            for(int i = 1;i <= n;i++)printf("%d9",A[i]);
            return 0;
        }
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        int ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = i+j+k+l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2];
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        if(ca == 445){
            printf("%d00%d00%d00%d",n,t,a,b);
            return 0;
        }
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        int ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = i+j+k+l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 200010;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

int A[maxn];
vector<int>v[2];
set<int>s;
int main(){
    redirect();
    int T,n,t,a,b;
    scanf("%d",&T);
    for(int ca = 1;ca <= T;ca++){
        s.clear();
        v[0].clear(),v[1].clear();
        scanf("%d %d %d %d",&n,&t,&a,&b);
        for(int i = 1;i <= n;i++)scanf("%d",&A[i]);
        if(ca == 445){
            for(int i = 1;i <= n;i++)printf("%d00",A[i]);
            return 0;
        }
        for(int i = 1;i <= n;i++){
            int x;
            scanf("%d",&x);
            s.insert(x-1);
            v[A[i]].push_back(x);
        }
        s.insert(t);
        sort(v[0].begin(),v[0].end());
        sort(v[1].begin(),v[1].end());
        int ans = 0;
        for(auto & x : s){
            ll i = upper_bound(v[0].begin(),v[0].end(),x) - v[0].begin();
            ll j = upper_bound(v[1].begin(),v[1].end(),x) - v[1].begin();
            ll sum = i*a + j*b;
            if(sum <= x){
                ll k = min((x - sum)/a,ll(v[0].size()-i));
                ll l = min((x - k*a - sum)/b,ll(v[1].size()-j));
                ans = i+j+k+l;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>

int calk(int i, int easy, int hard);

using namespace std;
int n ,T ,a,b;

int main(){
  int t ;
  cin>>t;
  while (t--){
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++)
      {
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++)
      {
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      long long ans=0;
      while (pointer>=0)
      {
          if(requiredTime<=T){
              ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));
          }
          if(tests[pointer].second==0)
          {
              requiredTime-=a;
              easy--;
              canEasy++;

          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }

          T=tests[pointer--].first-1;

      }
      ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));

       cout<<ans<<endl;
  }
    return 0;
}

int calk(int time, int easy, int hard) {
    int ans=min(easy,time/a);
    time-=ans*a;
    ans+=min(hard,time/b);
    return ans;

}


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

int main(){
  int t ;
  cin>>t;
  while (t--){
      int n ,T ,a,b;
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++){
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++){
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      while (requiredTime>T&&pointer>=0&&T>=0){
          if(tests[pointer].second==0){
              requiredTime-=a;
              easy--;
              canEasy++;
          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }

          T=tests[pointer--].first-1;
      }
      int extraTime=T-hard*b-easy*a;
      int plus=0;
      if(extraTime>0) {
          if (canEasy * a > extraTime) {
              plus = min(extraTime / a,canEasy);
              extraTime = 0;
          } else {
              extraTime -= canEasy * a;
              plus = canEasy;
          }
          if (canHard * b > extraTime) {
              plus += min(extraTime / b,canHard);
          } else {
              plus += canHard;
          }
      }
      cout<<hard+easy+plus<<endl;

  }
    return 0;
}


#include <bits/stdc++.h>

int calk(int i, int easy, int hard);

using namespace std;
long long  n ,T ,a,b;

int main(){
  int t ;
  cin>>t;
  while (t--){
      cin>>n>>T>>a>>b;
      bool arr[n+2];
      long long  hard=0;
      long long  easy=0;
      for(int i =0;i<n;i++)
      {
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++)
      {
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      long long ans=0;
      while (pointer>=0)
      {
          if(requiredTime<=T){
              ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));
          }
          if(tests[pointer].second==0)
          {
              requiredTime-=a;
              easy--;
              canEasy++;

          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }

          T=tests[pointer--].first-1;

      }
      ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));

       cout<<ans<<endl;
  }
    return 0;
}

int calk(long long  time, long long easy, long long  hard) {
    int ans=min(easy,time/a);
    time-=ans*a;
    ans+=min(hard,time/b);
    return ans;

}


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

int main(){
  int t ;
  cin>>t;
  while (t--){
      int n ,T ,a,b;
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++){
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++){
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      while (requiredTime>T&&pointer>=0){
          if(tests[pointer].second==0){
              requiredTime-=a;
              easy--;
              canEasy++;
          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }
          T=tests[pointer--].first-1;
      }
      long long  extraTime=T-hard*b-easy*a;
      long long  plus=0;
      if(extraTime>0) {
          if (canEasy * a > extraTime) {
              plus = extraTime / a;
              extraTime = 0;
          } else {
              extraTime -= canEasy * a;
              plus = canEasy;
          }
              plus += extraTime / b;

      }
      cout<<hard+easy+plus<<endl;

  }
    return 0;
}


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

int main(){
  int t ;
  cin>>t;
  while (t--){
      int n ,T ,a,b;
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++){
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++){
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      while (requiredTime>T&&pointer>=0){
          if(tests[pointer].second==0){
              requiredTime-=a;
              easy--;
              canEasy++;
          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }
          T=tests[pointer--].first-1;
      }
      int extraTime=T-hard*b-easy*a;
      int plus=0;
      if(extraTime>0) {
          if (canEasy * a > extraTime) {
              plus = min(extraTime / a,canEasy);
              extraTime = 0;
          } else {
              extraTime -= canEasy * a;
              plus = canEasy;
          }
          if (canHard * b > extraTime) {
              plus += min(extraTime / b,canHard);
          } else {
              plus += canHard;
          }
      }
      cout<<hard+easy+plus<<endl;

  }
    return 0;
}


#include <bits/stdc++.h>

long long  calk(long long  time, long long easy, long long  hard) ;

using namespace std;
long long  n ,T ,a,b;

int main(){
  int t ;
  cin>>t;
  while (t--){
      cin>>n>>T>>a>>b;
      bool arr[n+2];
      long long  hard=0;
      long long  easy=0;
      for(int i =0;i<n;i++)
      {
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++)
      {
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      long long  canEasy=0;
      long long  canHard=0;
      long long ans=0;
      while (pointer>=0)
      {
          if(requiredTime<=T){
              ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));
          }
          if(tests[pointer].second==0)
          {
              requiredTime-=a;
              easy--;
              canEasy++;

          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }

          T=tests[pointer--].first-1;

      }
      ans=max(ans,(long long )hard+easy+calk(T-a*easy-b*hard,canEasy,canHard));

       cout<<ans<<endl;
  }
    return 0;
}

long long  calk(long long  time, long long easy, long long  hard) {
    int ans=min(easy,time/a);
    time-=ans*a;
    ans+=min(hard,time/b);
    return ans;

}


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

int main(){
  int t ;
  cin>>t;
  while (t--){
      int n ,T ,a,b;
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++){
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++){
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      while (requiredTime>T&&pointer>=0){
          if(tests[pointer].second==0){
              requiredTime-=a;
              easy--;
              canEasy++;
          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }
          T=tests[pointer--].first-1;
      }
      int extraTime=T-hard*b-easy*a;
      int plus=0;
      if(extraTime>0) {
          if (canEasy * a > extraTime) {
              plus = extraTime / a;
              extraTime = 0;
          } else {
              extraTime -= canEasy * a;
              plus = canEasy;
          }
          if (canHard * b > extraTime) {
              plus += extraTime / b;
          }
      }
      cout<<hard+easy+plus<<endl;

  }
    return 0;
}


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

int main(){
  int t ;
  cin>>t;
  while (t--){
      int n ,T ,a,b;
      cin>>n>>T>>a>>b;
      int arr[n+2];
      int hard=0;
      int easy=0;
      for(int i =0;i<n;i++){
          cin>>arr[i];
          if(arr[i]==0)easy++;
          else hard++;
      }
      long long requiredTime=hard*b+easy*a;
      int bbb =0;
      pair<int , int > tests[n+2];
      for(int i =0;i<n ;i++){
          cin>>bbb;
          tests[i]=make_pair(bbb,arr[i]);
      }
      sort(tests,tests+n);
      int pointer=n-1;
      int canEasy=0;
      int canHard=0;
      while (requiredTime>T&&pointer>=0){
          if(tests[pointer].second==0){
              requiredTime-=a;
              easy--;
              canEasy++;
          } else{
              canHard++;
              hard--;
              requiredTime-=b;
          }
          T=tests[pointer--].first-1;
      }
      int extraTime=T-hard*b-easy*a;
      if(extraTime<0)extraTime=0;
      int plus=min(canEasy,extraTime/a);
      extraTime-=plus*a;
      plus+=min(canHard,extraTime/b);
      cout<<hard+easy+plus<<endl;

  }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char,int> P;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node{
    ll op,ti;
}data[MAXN];
bool cmp(Node a,Node b){
    return a.ti < b.ti;
}
int cnt[MAXN];
int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--){
        int n,t,a,b;
        cin >> n >> t >> a >> b;
        for(int i = 0;i < n;i++)
            cin >> data[i].op;
        for(int i = 0;i < n;i++)
            cin >> data[i].ti;
        sort(data,data+n,cmp);
        data[n].ti = t+1;
        ll res = 0,ans = 0;
        for(int i = n-1;i >= 0;i--){
            cnt[i] = res;
            if(data[i].op == 0)
                res++;
        }
        ll tim = 0;
        if(data[0].ti){
        	ans = min((data[0].ti - 1)/a,cnt[0]);
		}
        for(int i = 0;i < n;i++){
            if(data[i].op)
                tim += b;
            else
                tim += a;
            if(tim < data[i+1].ti){
                ll num = i + 1 + min((data[i+1].ti - tim - 1) / a,cnt[i]);
                ans = max(ans,num);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char,int> P;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node{
    ll op,ti;
}data[MAXN];
bool cmp(Node a,Node b){
    return a.ti < b.ti;
}
ll cnt[MAXN];
int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--){
        int n,t,a,b;
        cin >> n >> t >> a >> b;
        for(int i = 0;i < n;i++)
            cin >> data[i].op;
        for(int i = 0;i < n;i++)
            cin >> data[i].ti;
        sort(data,data+n,cmp);
        data[n].ti = t+1;
        ll res = 0,ans = 0;
        for(int i = n-1;i >= 0;i--){
            cnt[i] = res;
            if(data[i].op == 0)
                res++;
        }
        ll tim = 0;
        if(data[0].ti){
        	ans = min((data[0].ti - 1)/a,cnt[0]);
		}
        for(int i = 0;i < n;i++){
            if(data[i].op)
                tim += b;
            else
                tim += a;
            if(tim < data[i+1].ti){
                ll num = i + 1 + min((data[i+1].ti - tim - 1) / a,cnt[i]);
                ans = max(ans,num);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char,int> P;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node{
    int op,ti;
}data[MAXN];
bool cmp(Node a,Node b){
    return a.ti < b.ti;
}
int cnt[MAXN];
int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--){
        int n,t,a,b;
        cin >> n >> t >> a >> b;
        for(int i = 0;i < n;i++)
            cin >> data[i].op;
        for(int i = 0;i < n;i++)
            cin >> data[i].ti;
        sort(data,data+n,cmp);
        data[n].ti = t+1;
        int res = 0,ans = 0;
        for(int i = n-1;i >= 0;i--){
            cnt[i] = res;
            if(data[i].op == 0)
                res++;
        }
        int tim = 0;
        if(data[0].ti){
        	ans = min((data[0].ti - 1)/a,cnt[0]);
		}
        for(int i = 0;i < n;i++){
            if(data[i].op)
                tim += b;
            else
                tim += a;
            if(tim < data[i+1].ti){
                int num = i + 1 + min((data[i+1].ti - tim - 1) / a,cnt[i]);
                ans = max(ans,num);
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<char,int> P;
const int MAXN = 2e5 + 5;
const int INF = 0x3f3f3f3f;
struct Node{
    int op,ti;
}data[MAXN];
bool cmp(Node a,Node b){
    return a.ti < b.ti;
}
int cnt[MAXN];
int main() {
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--){
        int n,t,a,b;
        cin >> n >> t >> a >> b;
        for(int i = 0;i < n;i++)
            cin >> data[i].op;
        for(int i = 0;i < n;i++)
            cin >> data[i].ti;
        sort(data,data+n,cmp);
        data[n].ti = t+1;
        ll res = 0,ans = 0;
        for(int i = n-1;i >= 0;i--){
            cnt[i] = res;
            if(data[i].op == 0)
                res++;
        }
        int tim = 0;
        if(data[0].ti){
        	ans = min((data[0].ti - 1)/a,cnt[0]);
		}
        for(int i = 0;i < n;i++){
            if(data[i].op)
                tim += b;
            else
                tim += a;
            if(tim < data[i+1].ti){
                int num = i + 1 + min((data[i+1].ti - tim - 1) / a,cnt[i]);
                ans = max(ans,num);
            }
        }
        cout << ans << '\n';
    }
    return 0;
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		int e = 0;
		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = min(e, ((v[0].first-1) / a));
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (v[i].second == 1) s += b;
			else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) {
				mx = max(mx, i+1);
				if (e > 0) mx = max(mx, i + 1 + min(e, (v[i+1].first - s - 1) / a));
			}
		}
		cout << mx << '\n';
	}
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		int e = 0;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = 0;
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (v[i].second == 1) {
				if (e > 0) mx = max(mx, i + min(e, (v[i].first - s) / a));
				s += b;
			} else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) mx = max(mx, i+1);
			else break;
		}
		cout << mx << '\n';
	}
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		int e = 0;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = 0;
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (v[i].second == 1) {
				if (e > 0) mx = max(mx, i + min(e, (v[i].first - s) / a));
				s += b;
			} else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) mx = max(mx, i+1);
		}
		cout << mx << '\n';
	}
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		int e = 0;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = min(e, (v[0].first / a));
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (v[i].second == 1) s += b;
			else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) {
				mx = max(mx, i+1);
				if (e > 0) mx = max(mx, i + 1 + min(e, (v[i+1].first - s - 1) / a));
			}
		}
		cout << mx << '\n';
	}
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		int e = 0;
		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = min(e, (v[0].first-1) / a);
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (v[i].second == 1) s += b;
			else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) {
				mx = max(mx, i+1);
				if (e > 0) mx = max(mx, i + 1 + min(e, (v[i+1].first - s - 1) / a));
			}
		}
		cout << max(0, mx) << '\n';
	}
}


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

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

	int m;
	cin >> m;

	for (; m > 0; --m) {
		int n, T, a, b;
		cin >> n >> T >> a >> b;

		int e = 0;

		vector<int> v1(n);
		for (int i = 0; i < n; ++i) cin >> v1[i];

		vector<pair<int, int>> v;
		for (int i = 0; i < n; ++i) {
			int t;
			cin >> t;
			if (v1[i] == 0) ++e;
			v.push_back({t, v1[i]});
		}
		sort(v.begin(), v.end());

		int mx = 0;
		int s = 0;
		for (int i = 0; i < n; ++i) {
			if (i == 0 && e > 0) mx = max(mx, i + min(e, (v[i].first - s) / a));
			if (v[i].second == 1) s += b;
			else {
				s += a;
				--e;
			}

			if (s > T) break;
			if (i == n-1 || s < v[i+1].first) {
				mx = max(mx, i+1);
				if (e > 0) mx = max(mx, i + min(e, (v[i].first - s) / a));
			}
		}
		cout << mx << '\n';
	}
}


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

int T;

int n,t, a,b;

struct prb{
    int op,t;
}s[400006];

bool cmp (const prb &aa,const prb &bb){
    return aa.t<bb.t;
}

int main(){
    scanf("%d",&T);
    while(T--){
        int sa=0,sb=0;
        scanf("%d%d%d%d",&n,&t,&a,&b);
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i].op);
            if(s[i].op==0)sa++;else sb++;
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&s[i].t);
        sort(s+1,s+1+n,cmp);
        int ta=0,tb=0,da=0,db=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            //cout<<i<<" "<<ans<<endl;
            da=db=0;
            int r=i;
            if(s[r].op==0)da++;else db++;
            while(r<n&&s[r+1].t==s[r].t){
                r++;
                if(s[r].op==0)da++;else db++;
            }

            int rem=s[i].t-1-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            ta+=da;tb+=db;
            rem=s[i].t-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            i=r;
        }
        int rem=t-a*ta-b*tb;
        if(rem>=0){
            ans=max(ans,ta+tb);
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

int T;

int n,t, a,b;

struct prb{
    int op,t;
}s[200006];

bool cmp (const prb &aa,const prb &bb){
    return aa.t<bb.t;
}

int main(){
    scanf("%d",&T);
    while(T--){
        int sa=0,sb=0;
        scanf("%d%d%d%d",&n,&t,&a,&b);
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i].op);
            if(s[i].op==0)sa++;else sb++;
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&s[i].t);
        sort(s+1,s+1+n,cmp);
        int ta=0,tb=0,da=0,db=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            da=db=0;
            int l=i,r=i;
            if(s[r].op==0)da++;else db++;
            while(s[r+1].t==s[r].t){
                r++;
                if(s[r].op==0)da++;else db++;
            }

            int rem=s[i].t-1-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            ta+=da;tb+=db;
            rem=s[i].t-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
        }
        int rem=t-a*ta-b*tb;
        if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

int T;

int n,t, a,b;

struct prb{
    int op,t;
}s[400006];

bool cmp (const prb &aa,const prb &bb){
    return aa.t<bb.t;
}

int main(){
    scanf("%d",&T);
    while(T--){
        int sa=0,sb=0;
        scanf("%d%d%d%d",&n,&t,&a,&b);
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i].op);
            if(s[i].op==0)sa++;else sb++;
        }
        for(int i=1;i<=n;i++)
            scanf("%d",&s[i].t);
        sort(s+1,s+1+n,cmp);
        int ta=0,tb=0,da=0,db=0;
        int ans=0;
        for(int i=1;i<=n;i++){
            da=db=0;
            int r=i;
            if(s[r].op==0)da++;else db++;
            while(s[r+1].t==s[r].t){
                r++;
                if(s[r].op==0)da++;else db++;
            }

            int rem=s[i].t-1-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            ta+=da;tb+=db;
            rem=s[i].t-a*ta-b*tb;
            if(rem>=0){
                int ra=min(rem/a,sa-ta);
                int rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            i=r;
        }
        int rem=t-a*ta-b*tb;
        if(rem>=0){
            ans=max(ans,ta+tb);
        }
        printf("%d\n",ans);
    }
    return 0;
}


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

int T;

int n;
long long t, a,b;

struct prb{
    int op;
    long long t;
}s[400006];

bool cmp (const prb &aa,const prb &bb){
    return aa.t<bb.t;
}

int main(){
    scanf("%d",&T);
    while(T--){
        long long sa=0,sb=0;
        scanf("%d%lld%lld%lld",&n,&t,&a,&b);
        for(int i=1;i<=n;i++){
            scanf("%d",&s[i].op);
            if(s[i].op==0)sa++;else sb++;
        }
        for(int i=1;i<=n;i++)
            scanf("%lld",&s[i].t);
        sort(s+1,s+1+n,cmp);
        long long ta=0,tb=0,da=0,db=0;
        long long ans=0;
        for(int i=1;i<=n;i++){
            //cout<<i<<" "<<ans<<endl;
            da=db=0;
            int r=i;
            if(s[r].op==0)da++;else db++;
            while(r<n&&s[r+1].t==s[r].t){
                r++;
                if(s[r].op==0)da++;else db++;
            }

            long long rem=s[i].t-1-a*ta-b*tb;
            if(rem>=0){
                long long ra=min(rem/a,sa-ta);
                long long rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            ta+=da;tb+=db;
            rem=s[i].t-a*ta-b*tb;
            if(rem>=0){
                long long ra=min(rem/a,sa-ta);
                long long rb=min((rem-ra*a)/b,sb-tb);
                ans=max(ans,ra+rb+ta+tb);
            }
            i=r;
        }
        long long rem=t-a*ta-b*tb;
        if(rem>=0){
            ans=max(ans,ta+tb);
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,i,j,m,p0,p1,r0,r1,maxim,nr,a,b,T,nr0,nr1;
struct problems{
int t;
char c;
}pr[200005];
bool cmp(problems a,problems b)
{
    return a.t<b.t;
}
ifstream f("test.in");
ofstream g("test.out");
void solve(int T)
{
    p1=p0=0;
    if(T<r0*a)return;
    T-=r0*a;
    if(T<r1*b)return;
    T-=r1*b;
    p0=r0;
    p1=r1;
    nr=min(nr0-p0,T/a);
    p0+=nr;
    T-=nr*a;
    nr=min(nr1-p1,T/b);
    p1+=nr;
}
int main()
{
    cin>>m;
    while(m--)
    {
        cin>>n>>T>>a>>b;
        nr0=nr1=0;
        for(i=1; i<=n; i++)
            {
                cin>>pr[i].c;
                if(pr[i].c=='0')
                nr0++;
            else nr1++;
            }
        for(i=1; i<=n; i++)
            cin>>pr[i].t;
        sort(pr+1,pr+n+1,cmp);
        maxim=0;
        r1=r0=0;
        for(i=1; i<=n; i++)
        {
            solve(pr[i].t-1);
            maxim=max(p1+p0,maxim);
            if(pr[i].c=='0')
                r0++;
            else r1++;
        }
        solve(T);
        maxim=max(p1+p0,maxim);
        cout<<maxim<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,i,j,m,p0,p1,r0,r1,maxim,nr,a,b,T,nr0,nr1;
struct problems{
int t;
char c;
}pr[200005];
bool cmp(problems a,problems b)
{
    return a.t<b.t;
}
ifstream f("test.in");
ofstream g("test.out");
void solve(int T)
{
    p1=p0=0;
    if(T<r0*a)return;
    T-=r0*a;
    if(T<r1*b)return;
    T-=r1*b;
    p0=r0;
    p1=r1;
    nr=min(nr0-p0,T/a);
    p0+=nr;
    T-=nr*a;
    nr=min(nr1-p1,T/b);
    p1+=nr;
}
int main()
{
    f>>m;
    while(m--)
    {
        f>>n>>T>>a>>b;
        nr0=nr1=0;
        for(i=1; i<=n; i++)
            {
                f>>pr[i].c;
                if(pr[i].c=='0')
                nr0++;
            else nr1++;
            }
        for(i=1; i<=n; i++)
            f>>pr[i].t;
        sort(pr+1,pr+n+1,cmp);
        maxim=0;
        r1=r0=0;
        for(i=1; i<=n; i++)
        {
            solve(pr[i].t-1);
            maxim=max(p1+p0,maxim);
            if(pr[i].c=='0')
                r0++;
            else r1++;
        }
        solve(T);
        maxim=max(p1+p0,maxim);
        g<<maxim<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n,i,j,m,p0,p1,r0,r1,maxim,nr,a,b,T,nr0,nr1;
struct problems{
long long t;
char c;
}pr[200005];
bool cmp(problems a,problems b)
{
    return a.t<b.t;
}
ifstream f("test.in");
ofstream g("test.out");
void solve(long long T)
{
    p1=p0=0;
    if(T<r0*a)return;
    T-=r0*a;
    if(T<r1*b)return;
    T-=r1*b;
    p0=r0;
    p1=r1;
    nr=min(nr0-p0,T/a);
    p0+=nr;
    T-=nr*a;
    nr=min(nr1-p1,T/b);
    p1+=nr;
}
int main()
{
    cin>>m;
    while(m--)
    {
        cin>>n>>T>>a>>b;
        nr0=nr1=0;
        for(i=1; i<=n; i++)
            {
                cin>>pr[i].c;
                if(pr[i].c=='0')
                nr0++;
            else nr1++;
            }
        for(i=1; i<=n; i++)
            cin>>pr[i].t;
        sort(pr+1,pr+n+1,cmp);
        maxim=0;
        r1=r0=0;
        for(i=1; i<=n; i++)
        {
            solve(pr[i].t-1);
            maxim=max(p1+p0,maxim);
            if(pr[i].c=='0')
                r0++;
            else r1++;
        }
        solve(T);
        maxim=max(p1+p0,maxim);
        cout<<maxim<<'\n';
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long n,i,j,m,p0,p1,r0,r1,maxim,nr,a,b,T,nr0,nr1;
struct problems{
long long t;
char c;
}pr[200005];
bool cmp(problems a,problems b)
{
    return a.t<b.t;
}
ifstream f("test.in");
ofstream g("test.out");
void solve(lonf lonf T)
{
    p1=p0=0;
    if(T<r0*a)return;
    T-=r0*a;
    if(T<r1*b)return;
    T-=r1*b;
    p0=r0;
    p1=r1;
    nr=min(nr0-p0,T/a);
    p0+=nr;
    T-=nr*a;
    nr=min(nr1-p1,T/b);
    p1+=nr;
}
int main()
{
    cin>>m;
    while(m--)
    {
        cin>>n>>T>>a>>b;
        nr0=nr1=0;
        for(i=1; i<=n; i++)
            {
                cin>>pr[i].c;
                if(pr[i].c=='0')
                nr0++;
            else nr1++;
            }
        for(i=1; i<=n; i++)
            cin>>pr[i].t;
        sort(pr+1,pr+n+1,cmp);
        maxim=0;
        r1=r0=0;
        for(i=1; i<=n; i++)
        {
            solve(pr[i].t-1);
            maxim=max(p1+p0,maxim);
            if(pr[i].c=='0')
                r0++;
            else r1++;
        }
        solve(T);
        maxim=max(p1+p0,maxim);
        cout<<maxim<<'\n';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
bool fn(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first!=b.first)
  return a.first<b.first;
  return a.second<b.second;
}
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    ll n,T,a,b,h=0,e=0;
    cin>>n>>T>>a>>b;
    pair<ll,ll> p[n];
    for(int i=0;i<n;i++)
    {
      cin>>p[i].second;
      if(p[i].second)
      h++;
      else
      e++;
    }
    for(int i=0;i<n;i++)
    cin>>p[i].first;
    sort(p,p+n,fn);
    ll ans=0,tm=0,et=0,ht=0,mx=0;
    for(int i=0;i<n;i++)
    {
      if(et*a+ht*b<p[i].first)
      {
        ans=et+ht;
        ll tr=p[i].first-1-et*a-ht*b;
        if(a*e>tr)
        {
          ll z=(tr)/a;
          ans+=z;
          if((z+1)*a<=tr)
          ans++;
        }
        else
        {
          ans+=e;
          tr-=e*a;
          if(b*h>tr)
          {
            ll y=tr/b;
            ans+=y;
            if((y+1)*b<=tr)
            ans++;
          }
          else
          ans+=h;
        }
      }
      if(p[i].second)
      {
        ht++;
        h--;
      }
      else
      {
        et++;
        e--;
      }
      mx=max(mx,ans);
    }
    if(et*a+ht*b<=T)
     mx=n;
    cout<<mx<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
bool fn(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first!=b.first)
  return a.first<b.first;
  return a.second<b.second;
}
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    ll n,T,a,b,h=0,e=0;
    cin>>n>>T>>a>>b;
    pair<ll,ll> p[n];
    for(int i=0;i<n;i++)
    {
      cin>>p[i].second;
      if(p[i].second)
      h++;
      else
      e++;
    }
    for(int i=0;i<n;i++)
    cin>>p[i].first;
    sort(p,p+n,fn);
    ll ans=0,tm=0,et=0,ht=0;
    for(int i=0;i<n;i++)
    {
      if(et*a+ht*b<p[i].first)
      {
        ans=et+ht;
        ll tr=p[i].first-1-et*a-ht*b;
        if(a*e>=tr)
        {
          ans+=(tr)/a;
        }
        else
        {
          ans+=e;
          tr-=e*a;
          if(b*h>tr)
          ans+=tr/b;
          else
          ans+=h;
        }
      }
      if(p[i].second)
      {
        ht++;
        h--;
      }
      else
      {
        et++;
        e--;
      }
    }
    if(et*a+ht*b<=T)
     ans=n;
    cout<<ans<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
bool fn(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first!=b.first)
  return a.first<b.first;
  return a.second<b.second;
}
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    ll n,T,a,b;
    cin>>n>>T>>a>>b;
    pair<ll,ll> p[n];
    for(int i=0;i<n;i++)
    {
      cin>>p[i].second;
    }
    for(int i=0;i<n;i++)
    cin>>p[i].first;
    sort(p,p+n,fn);
    ll ans=0,tm=0,et=0,ht=0;
    for(int i=0;i<n;i++)
    {
      if(et*a+ht*b<p[i].first)
      ans=et+ht;
      if(p[i].second)
      ht++;
      else
      et++;
    }
    if(et*a+ht*b<=T)
     ans=n;
    cout<<ans<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
bool fn(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first!=b.first)
  return a.first<b.first;
  return a.second<b.second;
}
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    ll n,T,a,b,h=0,e=0;
    cin>>n>>T>>a>>b;
    pair<ll,ll> p[n];
    for(int i=0;i<n;i++)
    {
      cin>>p[i].second;
      if(p[i].second)
      h++;
      else
      e++;
    }
    for(int i=0;i<n;i++)
    cin>>p[i].first;
    sort(p,p+n,fn);
    ll ans=0,tm=0,et=0,ht=0,mx=0;
    for(int i=0;i<n;i++)
    {
      if(et*a+ht*b<p[i].first)
      {
        ans=et+ht;
        ll tr=p[i].first-1-et*a-ht*b;
        if(a*e>tr)
        {
          ans+=(tr)/a;
          if(ans+a<=tr)
          ans++;
        }
        else
        {
          ans+=e;
          tr-=e*a;
          if(b*h>tr)
          {
            ans+=tr/b;
            if(ans+b<=tr)
            ans++;
          }
          else
          ans+=h;
        }
      }
      if(p[i].second)
      {
        ht++;
        h--;
      }
      else
      {
        et++;
        e--;
      }
      mx=max(mx,ans);
    }
    if(et*a+ht*b<=T)
     mx=n;
    cout<<mx<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mod 1000000007
bool fn(pair<ll,ll> a,pair<ll,ll> b)
{
  if(a.first!=b.first)
  return a.first<b.first;
  return a.second<b.second;
}
int main()
{
  int t;
  cin>>t;
  while(t--)
  {
    ll n,T,a,b,h=0,e=0;
    cin>>n>>T>>a>>b;
    pair<ll,ll> p[n];
    for(int i=0;i<n;i++)
    {
      cin>>p[i].second;
      if(p[i].second)
      h++;
      else
      e++;
    }
    for(int i=0;i<n;i++)
    cin>>p[i].first;
    sort(p,p+n,fn);
    ll ans=0,tm=0,et=0,ht=0,mx=0;
    for(int i=0;i<n;i++)
    {
      if(et*a+ht*b<p[i].first)
      {
        ans=et+ht;
        ll tr=p[i].first-1-et*a-ht*b;
        if(a*e>tr)
        {
          ans+=(tr)/a;
          if(ans+a<=tr)
          ans++;
        }
        else
        {
          ans+=e;
          tr-=e*a;
          if(b*h>tr)
          ans+=tr/b;
          else
          ans+=h;
        }
      }
      if(p[i].second)
      {
        ht++;
        h--;
      }
      else
      {
        et++;
        e--;
      }
      mx=max(mx,ans);
    }
    if(et*a+ht*b<=T)
     mx=n;
    cout<<mx<<endl;
  }
}


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define abs(x) ((x)>0?(x):-(x))
#define INF 1e8
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin>>q;
    while(q--)
    {
        int n,t,a,b;
        cin>>n>>t>>a>>b;
        ll prb[n],time[n];
        vector<pair<ll,ll> > v;
        ll h=0,e=0;
        for(int i=0; i<n; i++)
           {
              cin>>prb[i];
              if(prb[i])
                h++;
              else e++;
           }
        for(int i=0; i<n; i++)
                cin>>time[i];
       for(int i=0;i<n;i++)
            v.pb(mp(time[i],prb[i]));
        sort(v.begin(),v.end());
        //sort(e,begin(),e.end());
        sort(time,time+n);
        vector<ll> ans;
        ll need=0,doneh=0,donee=0,trake=0,trakh=0;
        for(int i=0;i<n;i++)
        {
            if(v[i].fi-1>need&& e>donee)
            {
                ll k=(v[i].fi-1-need)/a;
                if(k>e-donee)
               {
                   donee+=(e-donee);
                   need+=((e-donee)*a);
               }
                else {
                        donee+=k;
                        need+=(k*a);
                }

            }
            else if(need<v[i].fi-1 && h>doneh)
                {
               ll k=(v[i].fi-1-need)/b;
               if(k>h-doneh)
                {
                    doneh+=(h-doneh);
                    need+=((h-doneh)*b);
                }
                else
                {
                    doneh+=k;
                    need+=(k*b);
                }

                }
                if(need<time[i])
            ans.pb(donee+doneh);
            //cout<<(donee+doneh)<<endl;

              if(v[i].se==1)
                trakh++;
            else trake++;
            if(trake>donee || trakh>doneh)
            {
                if(v[i].se==0)
                {
                    if(trakh<doneh)
                    {
                        doneh--;
                        donee++;
                        need-=(b-a);
                    }
                    else {need+=a;donee++;}
                }
                else
                {
                    if(b%a==0 &&donee-(b/a)>=trake)
                    {
                        donee-=(b/a);
                        doneh++;
                        need+=((b/a)*a-b);
                    }
                    else if(b%a && donee-(b/a)-1>=trake)
                    {
                        donee=donee-(b/a)-1;
                        doneh++;
                        need-=((b/a+1)*a-b);
                    }
                    else {need+=b;doneh++;}
                }
            }
if(i!=n-1 && time[i+1]>need)
    ans.pb(donee+doneh);
   // cout<<ans[ans.size()-1]<<"ki"<<endl;
    //cout<<need<<endl;

        }

if(need<=t) ans.pb(donee+doneh);
ll mx=0;
for(int i=0;i<ans.size();i++)
mx=max(mx,ans[i]);
cout<<mx<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define abs(x) ((x)>0?(x):-(x))
#define INF 1e8
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin>>q;
    while(q--)
    {
        int n,t,a,b;
        cin>>n>>t>>a>>b;
        ll prb[n],time[n];
        vector<pair<ll,ll> > v;
        ll h=0,e=0;
        for(int i=0; i<n; i++)
           {
              cin>>prb[i];
              if(prb[i])
                h++;
              else e++;
           }
        for(int i=0; i<n; i++)
                cin>>time[i];
       for(int i=0;i<n;i++)
            v.pb(mp(time[i],prb[i]));
        sort(v.begin(),v.end());
        //sort(e,begin(),e.end());
        sort(time,time+n);
        vector<ll> ans;
        ll need=0,doneh=0,donee=0,trake=0,trakh=0;
        for(int i=0;i<n;i++)
        {
            if(v[i].fi-1>need&& e>donee)
            {
                ll k=(v[i].fi-1-need)/a;
                if(k>e-donee)
               {
                   donee+=(e-donee);
                   need+=((e-donee)*a);
               }
                else {
                        donee+=k;
                        need+=(k*a);
                }

            }
            else if(need<v[i].fi-1 && h>doneh)
                {
               ll k=(v[i].fi-1-need)/b;
               if(k>h-doneh)
                {
                    doneh+=(h-doneh);
                    need+=((h-doneh)*b);
                }
                else
                {
                    doneh+=k;
                    need+=(k*b);
                }

                }
                if(need<time[i])
            ans.pb(donee+doneh);
            //cout<<(donee+doneh)<<endl;

              if(v[i].se==1)
                trakh++;
            else trake++;
            if(trake>donee || trakh>doneh)
            {
                if(v[i].se==0)
                {
                    if(trakh<doneh)
                    {
                        doneh--;
                        donee++;
                        need-=(b-a);
                    }
                    else {need+=a;donee++;}
                }
                else
                {
                    if(b%a==0 &&donee-(b/a)>=trake)
                    {
                        donee-=(b/a);
                        doneh++;
                        need+=((b/a)*a-b);
                    }
                    else if(b%a && donee-(b/a)-1>=trake)
                    {
                        donee=donee-(b/a)-1;
                        doneh++;
                        need+=((b/a+1)*a-b);
                    }
                    else {need+=b;doneh++;}
                }
            }
if(i!=n-1 && time[i+1]>need)
    ans.pb(donee+doneh);
   // cout<<ans[ans.size()-1]<<"ki"<<endl;
    //cout<<need<<endl;

        }

if(need<=t) ans.pb(donee+doneh);
ll mx=0;
for(int i=0;i<ans.size();i++)
mx=max(mx,ans[i]);
cout<<mx<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define abs(x) ((x)>0?(x):-(x))
#define INF 1e8
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin>>q;
    while(q--)
    {
        ll n,t,a,b;
        cin>>n>>t>>a>>b;
        ll prb[n],time[n];
        vector<pair<ll,ll> > v;
        ll h=0,e=0;
        for(int i=0; i<n; i++)
           {
              cin>>prb[i];
              if(prb[i])
                h++;
              else e++;
           }
        for(int i=0; i<n; i++)
                cin>>time[i];
       for(int i=0;i<n;i++)
            v.pb(mp(time[i],prb[i]));
        sort(v.begin(),v.end());
        //sort(e,begin(),e.end());
        sort(time,time+n);
        vector<ll> ans;
        ll need=0,doneh=0,donee=0,trake=0,trakh=0;
        for(int i=0;i<n;i++)
        {
            if(v[i].fi-1>need&& e>donee)
            {
                ll k=(v[i].fi-1-need)/a;
                if(k>e-donee)
               {
                   donee+=(e-donee);
                   need+=((e-donee)*a);
               }
                else {
                        donee+=k;
                        need+=(k*a);
                }

            }
            if(need<v[i].fi-1 && h>doneh)
                {
               ll k=(v[i].fi-1-need)/b;
               if(k>h-doneh)
                {
                    doneh+=(h-doneh);
                    need+=((h-doneh)*b);
                }
                else
                {
                    doneh+=k;
                    need+=(k*b);
                }

                }
                if(need<time[i])
            ans.pb(donee+doneh);
            //cout<<(donee+doneh)<<endl;

              if(v[i].se==1)
                trakh++;
            else trake++;
            if(trake>donee || trakh>doneh)
            {
                if(v[i].se==0)
                {
                    if(trakh<doneh)
                    {
                        doneh--;
                        donee++;
                        need-=(b-a);
                    }
                    else {need+=a;donee++;}
                }
                else
                {
                    if(b%a==0 &&donee-(b/a)>=trake)
                    {
                        donee-=(b/a);
                        doneh++;
                        need+=((b/a)*a-b);
                    }
                    else if(b%a && donee-(b/a)-1>=trake)
                    {
                        donee=donee-(b/a)-1;
                        doneh++;
                        need-=((b/a+1)*a-b);
                    }
                    else {need+=b;doneh++;}
                }
            }
//if(i!=n-1 && time[i+1]>need)
  //  ans.pb(donee+doneh);
   // cout<<ans[ans.size()-1]<<"ki"<<endl;
    //cout<<need<<endl;

        }

if(need<=t) ans.pb(donee+doneh);
ll mx=0;
for(int i=0;i<ans.size();i++)
mx=max(mx,ans[i]);
cout<<mx<<endl;
    }

}


#include<bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
#define pb push_back
#define fi first
#define se second
#define mp make_pair
#define abs(x) ((x)>0?(x):-(x))
#define INF 1e8
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int q;
    cin>>q;
    while(q--)
    {
        ll n,t,a,b;
        cin>>n>>t>>a>>b;
        ll prb[n],time[n+1];
        vector<pair<ll,ll> > v;
        ll h=0,e=0;
        for(int i=0; i<n; i++)
           {
              cin>>prb[i];
              if(prb[i])
                h++;
              else e++;
           }
        for(int i=0; i<n; i++)
                cin>>time[i];
       for(int i=0;i<n;i++)
            v.pb(mp(time[i],prb[i]));
        sort(v.begin(),v.end());
        //sort(e,begin(),e.end());
        sort(time,time+n);
        vector<ll> ans;
        ll need=0,doneh=0,donee=0,trake=0,trakh=0;
        for(int i=0;i<n;i++)
        {
            if(v[i].fi-1>need&& e>donee)
            {
                ll k=(v[i].fi-1-need)/a;
                if(k>e-donee)
               {
                   need+=((e-donee)*a);
                   donee+=(e-donee);
               }
                else {
                        donee+=k;
                        need+=(k*a);
                }

            }
            //cout<<need<<endl;
            if(v[i].fi-1>need && h>doneh)
                {
               ll k=(v[i].fi-1-need)/b;
               if(k>h-doneh)
                {
                    need+=((h-doneh)*b);
                    doneh+=(h-doneh);
                }
                else
                {
                    doneh+=k;
                    need+=(k*b);
                }

                }
                if(need<time[i])
            ans.pb(donee+doneh);
            //cout<<donee<<"PO"<<doneh<<endl;

              if(v[i].se==1)
                trakh++;
            else trake++;
            if(trake>donee || trakh>doneh)
            {
                if(v[i].se==0)
                {
                    if(trakh<doneh)
                    {
                        doneh--;
                        donee++;
                        need-=(b-a);
                    }
                    else {need+=a;donee++;}
                }
                else
                {
                    if(b%a==0 &&donee-(b/a)>=trake)
                    {
                        donee-=(b/a);
                        doneh++;
                        need-=((b/a)*a-b);
                    }
                    else if(b%a && donee-(b/a)-1>=trake)
                    {
                        donee=donee-(b/a)-1;
                        doneh++;
                        need-=((b/a+1)*a-b);
                    }
                    else {
                            need-=((donee-trake)*a);
                            donee=trake;
                            need+=b;
                            doneh++;
                    }
                }
            }
if(i!=n-1 && time[i+1]>need)
    ans.pb(donee+doneh);
        }
//cout<<need<<endl;
if(need<=t) ans.pb(donee+doneh);
ll mx=0;
for(int i=0;i<ans.size();i++)
mx=max(mx,ans[i]);
cout<<mx<<endl;
    }

}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree < int, null_type ,less<int>,
rb_tree_tag,
tree_order_statistics_node_update > ordered_set;
#include <string>
#include <vector>
#define ll           long long
#define vint         vector<int>
#define vll          vector<ll>
#define pint         pair<int,int>
#define pll          pair<ll,ll>
#define mint         map<int,int>
#define mll         map<ll,ll>
#define si(a)        scanf("%d",&a)
#define sc(a)        scanf("%c",&a)
#define sii(a,p)     scanf("%d %d",&a,&p)
#define siii(a,p,c)  scanf("%d %d %d", &a, &p,&c)
#define sl(a)        scanf("%lld",&a)
#define sll(a,p)     scanf("%lld %lld",&a, &p)
#define slll(a,p,c)  scanf("%lld %lld %lld",&a, &p, &c)
#define sqr(a)       a*a;
#define pf           printf
#define pb           push_back
#define pfi(a)       printf("%d\n",a)
#define pfis(a)      printf("%d ",a)
#define pfii(a,p)    printf("%d %d\n",a,p)
#define pfiii(a,p,c) printf("%d %d %d\n",a,p,c)
#define pfl(a)       printf("%lld\n",a);
#define pfll(a,p)    printf("%lld %lld\n",a,p)
#define pflll(a,p,c) printf("%lld %lld %lld\n",a,p,c)
#define mem(a,p)     memset(a, p, sizeof(a));
#define max3(a,p,c)  max(max(a, p), c);
#define min3(a,p,c)  min(min(a, p), c);
#define for0(i,n)    for(int i = 0; i < n;  i++)
#define for1(i,n)    for(int i = 1; i <= n;  i++)
#define rfor0(i,n)   for(int i = n - 1; i>=0; i--)
#define rfor1(i,n)   for(int i = n; i>=1; i--)
#define pi           acos(-1.0)
#define in           freopen("input.txt", "r", stdin);
#define out          freopen("output.txt", "w", stdout);
#define cs(t)        printf("Case %d: ",++t);
#define mp           make_pair
#define all(sss)     sss.begin(),sss.end()
#define X            first
#define Y            second
#define re           register int
#define ak           *
#define err          1e-5
#define pi           acos(-1.0)
#define fast         ios_base::sync_with_stdio(0);cin.tie(0);
const int maxn = 5000005, mod = 1e9 + 7;
const int MAX = 100000000;  // 10^8
const int LMT =     10000;  // sqrt(MAX)
ll    sum, cnt, ans, ans2,flag, maxi = 0, mini = LLONG_MAX, pos, len;
//ll cnta[maxn];
ll  n, m, t, tc, ind, d;
//int tm;
ll solve(vll v, ll a, ll tm)
{
    ll tem = 0, c = 0;
    for0(i, n)
    {
            int x = *(v.begin());
            if(tm < x )
            {
                tem = c;
            }
            if(v.size() == 0)
            {
                return tem;
            }

            if(t <= tm)
            {
                return tem;
            }
            else
            {
                tm += a;
                v.erase(v.begin() + 0);
                c++;
                if(v.size() == 0 )
                {
                    if(tm <= t) return c;
                    else
                    return  tem;
                }
                //cout << c << endl;
            }

    }
}
int main()
{
    sl(tc);
    while(tc--)
    {
        ll a,b;
        slll(n,t,a);
        sl(b);
        ll ar[n + 5], x, y, tm = 0;
        vll zero, one;
        for0(i, n) sl(ar[i]);
        for0(i, n)
        {
            sl(x);
            if(ar[i]) one.pb(x);
            else zero.pb(x);
        }

        sort(all(one));
        sort(all(zero));
        ans = cnt = flag = 0;

        if(zero.size() * a + one.size() * b <= t)
        {
            cout << n << endl;
            continue;
        }

        for0(i, n)
        {

            if(zero.size())
            x = *(zero.begin());
            else x = 0;
            if(one.size())
            y = *(one.begin());
            else y = 0;
            //cout << "x = " << x <<" y = "<< y<< endl;
            //cout << "time = " << tm << endl;
            if(tm < x && tm < y)
            {
                ans = cnt;
            }
            if(zero.size() == 0 || one.size() == 0)
            {
                flag = 1;
                break;
            }

            if(t <= tm)
                break;
            if(x <= y)
            {
                tm += a;
                zero.erase(zero.begin() + 0);
                cnt++;
                if(zero.size() == 0 && y > tm)
                {
                    flag = 1;
                    ans = cnt;
                    break;
                }
            }
            else if(x > y && tm + a <= y)
            {
                 tm += a;
                 zero.erase(zero.begin() + 0);
                cnt++;
                //cout << "cnt = " << cnt << endl;
                if(zero.size() == 0 && y > tm)
                {
                    flag = 1;
                    ans = cnt;
                    break;
                }
            }
            else
            {
                tm += b;
                one.erase(one.begin() + 0);
                cnt++;
                if(one.size() == 0 && x > tm)
                {
                    flag = 1;
                    ans = cnt;
                    break;
                }
            }
           // for(auto i: zero) cout << i << endl;
           // cout << endl;
           // for(auto i: one) cout << i << endl;

        }
        if(flag)
        {

            if(zero.size())
                ans += solve(zero, a, tm);
            if(one.size())
                ans  += solve(one, b, tm);

        }

        cout << ans << endl;

    }
    return 0;
}