#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int, int> 
#define PLL pair<LL, LL> 

LL dp[5005];


int main()
{
	//ios_base::sync_with_stdio(0);
	int t;
	scanf("%d", &t);
	while(t--)
	{
		LL n;
		vector<int> X;
		int a, b, c;
		scanf("%lld%d%d%d", &n, &a, &b, &c);
		X.PB(a);
		X.PB(b);
		X.PB(c);
		sort(X.begin(), X.end());
		int ind=0;
		dp[0]=1;
		while(dp[ind]<n)
		{
			ind++;
			dp[ind]=0;
			if(ind<X[1])
			{
				dp[ind]=1;
				continue;
			}
			dp[ind]+=dp[ind-X[0]];
			dp[ind]+=dp[ind-X[1]];
			if(ind<X[2])
				continue;
			dp[ind]+=dp[ind-X[2]];
		}
		printf("%d\n", ind);
	}
	return 0;
}