#include <iostream> // strings/streams
#include <string>
#include <sstream>
#include <vector> // datastructures
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <bitset>
#include <tuple> // quick compare
#include <cstdio> // utils
#include <numeric>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <chrono>
#include <cassert>

#define REP(i,n)	for(auto i = decltype(n)(0); i<(n); i++)
#define F(v)		begin(v), end(v)
constexpr bool LOG =
#ifdef _LOG
true;
#define _GLIBCXX_DEBUG
#else
false;
#endif

using namespace std;
using ll = long long;
using ii = pair<int,int>;
using vi = vector<int>;
using vb = vector<bool>;
using vvi = vector<vi>;

constexpr int  INF = 1e9+1; // < 1e9 - -1e9
constexpr ll LLINF = 1e18+1;
void Log() { if(LOG) cerr << "\n"; }
template<class T, class... S> void Log(T t, S... s){
	if(LOG) cerr << t << "\t", Log(s...);
}
/* ============== END OF HEADER ============== */

int main(){
	ios_base::sync_with_stdio(false); cin.tie(nullptr);
	int TC; cin >> TC;
	while(TC--){
		ll N; int A, B, C; cin >> N >> A >> B >> C;
		vector<ll> M(10000,INF);
		M[0] = 1;
		if(N<= 1){
			cout << "0\n";
			continue;
		}
		for(int k = 1; k < 100000; ++k){
			long long P,Q;
			if(k - A >= 0)
				P = M[k-A];
			else P=0;
			if(k - B >= 0)
				Q = P + M[k-B];
			else Q = P;
			if(k - C >= 0)
				M[k] = Q + M[k-C];
			else M[k] = Q;
			M[k] = max(M[k], M[k-1]);
			//cerr << k <<":\t" << M[k] << "\n";
			if(M[k] >= N){
				cout << k << "\n";
				cout.flush();
				break;
			}
		}
	}
	return 0;
}