#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 1000006;

ll F[MAXN];
void solve() {
	ll n;
	int a,b,c;
	scanf("%lld%d%d%d",&n,&a,&b,&c);
	F[0]=1;
	int ind=0;
	while(1){
#define F(i) (i<0?0:F[i])
		F[ind]=max(1LL,F(ind-a)+F(ind-b)+F(ind-c));
	//	printf("%d ->%lld\n",ind,F[ind]);
		if(F[ind]>=n){printf("%d\n",ind);break;	}
		ind++;
	}
}

int main() {
//	freopen("input.in", "r", stdin);
//	freopen("output.out", "w", stdout);
	int t=1;
	scanf("%d",&t);
	fru(i,t) solve();
	return 0;
}