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


int main() {
    long T, N, A, B, C;
    cin >> T;
    while (T--) {
        cin >> N >> A >> B >> C;
        if (A > B) swap(A, B);
        if (B > C) swap(B, C);
        if (A > B) swap(A, B);
        vector<long> getfor(10000, 1);
        long price = 0;
        while (getfor[price] < N) {
            price++;
            long distinguished = 0;
            if (price >= B) distinguished += getfor[price-A] + getfor[price-B];
            if (price >= C) distinguished += getfor[price-C];
            if (distinguished) getfor[price] = distinguished;
        }
        cout << price << endl;
    }
}