#include <iostream>
#include <map>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long ll;

int p[3];
ll n;

const int MAXX = 64*100;
ll rec_f[MAXX];

ll f(int x) {
    if ( x < 0 )
        return 0;
    if ( x < p[1] )
        return 1;
    if (rec_f[x] != -1)
        return rec_f[x];

    return rec_f[x] = f(x-p[0]) + f(x-p[1]) + f(x-p[2]);
}

int main() {
    int T; cin >> T;
    while (T-->0){
        cin >> n;
        cin >> p[0] >> p[1] >> p[2];
        sort(p,p+3);
        
        memset(rec_f,-1,sizeof(rec_f));
         
        int x = 0;
        while(f(x)<n)
            x++;
        cout<<x<<endl;
    }
    return 0;
}