#include <bits/stdc++.h>

#define clr(x) memset((x), 0, sizeof((x)))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp std::make_pair
#define x first
#define y second

typedef std::pair<int, int> PII;
typedef int64_t ll;
typedef std::pair<ll, ll> PLL;
typedef long double ld;
typedef std::pair<ld, ld> PLD;
typedef std::pair<double, double> PDD;
using namespace std;

int nxt() {
    int a;
    scanf("%d", &a);
    return a;
}

const int N = 100000;
long long ans[N];

void solve() {
    long long n;
    cin >> n;
    int a[3];
    for (int i = 0; i < 3; ++i) {
        cin >> a[i];
    }
    sort(a, a + 3);
    if (n == 1) {
        cout << "0\n";
        return;
    }
    for (int i = 0; i < a[0]; ++i) {
        ans[i] = 1;
    }
    for (int i = a[0];i < N;++i) {
        ans[i] = 0;
        if (i - a[0] >= 0) {
            ans[i] += ans[i - a[0]];
        }
        if (i - a[1] >= 0) {
            ans[i] += ans[i - a[1]];
        }
        if (i - a[2] >= 0) {
            ans[i] += ans[i - a[2]];
        }
        if (ans[i] >= n) {
            cout << i << "\n";
            return;
        }
    }
    assert(false);
}

int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    double cl0 = clock();
#endif

    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        solve();
    }

#ifdef LOCAL
    cerr << "time: " << (clock() - cl0) / CLOCKS_PER_SEC << endl;
#endif
}