#include <iostream>
#include <algorithm>
#define MAX 1000000
using namespace std;
int t;
long long n,dp[MAX];
int a,b,c;
int main(){
	cin >> t;
	for(int nmnm=0; nmnm<t; nmnm++){
		cin >> n >> a >> b >> c;
		//cerr << a << ' ' << b << ' ' << c << '\n';
		int g=max(a,max(b,c));
		//cerr << g << ' ';
		dp[0]=1;
		for(int i=1; i<MAX; i++){
            if (n==1) {cout << 0 << '\n';break;}
			//if(i==1) cout << "hello";
			long long temp=0;
			if(i>=g) temp=dp[i-a]+dp[i-b]+dp[i-c];
			else{
				if(i>=a) temp+=dp[i-a];
				if(i>=b) temp+=dp[i-b];
				if(i>=c) temp+=dp[i-c];
			}
			dp[i]=max(dp[i-1], temp);
			//if(i==2) cout << temp << '\n';
			//cout << dp[1] << '\n';
			//if(i==1) cerr << "hello";
			//cerr << i << ' ' <<  dp[i] << '\n';
			if(dp[i]>=n){
				cout << i << '\n';
				break;
			}
		}
	}
}