#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

long howfar[100000];

int main() {
    howfar[0]=1;
    int T; cin >> T;
    while(T-->0) {
        long N; int A,B,C;
        cin >> N >> A >> B >> C;
        if(N==1) cout << 0 << endl; else
        for(int X=1; X<100000; X++) {
            howfar[X]=0;
            if(X>=A) howfar[X] += howfar[X-A];
            if(X>=B) howfar[X] += howfar[X-B];
            if(X>=C) howfar[X] += howfar[X-C];
            howfar[X] = max(howfar[X],1L);
            //cout << X << " -> " << howfar[X] << endl;
            if(howfar[X]>=N) {
                cout << X << endl;
                break;
            }
         }
    }
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}