#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cstring>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;

#define SIZE(x) (int((x).size()))
#define rep(i,l,r) for (int i=(l); i<=(r); i++)
#define repd(i,r,l) for (int i=(r); i>=(l); i--)
#define rept(i,c) for (typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)

#ifndef ONLINE_JUDGE
#define debug(x) { cerr<<#x<<" = "<<(x)<<endl; }
#else
#define debug(x) {}
#endif

LL f[200000];

void lemon()
{
	LL n; int p1,p2,p3; scanf("%lld%d%d%d",&n,&p1,&p2,&p3);
	if (n==1) { printf("0\n"); return; }
	int i=min(p1,min(p2,p3)); 
	rep(k,0,i-1) f[k]=0; 
	f[i]=(i==p1?1:0)+(i==p2?1:0)+(i==p3?1:0);
	while (1)
	{
		if (f[i]>=n)
		{
			printf("%d\n",i);
			return;
		}
		i++;
		f[i]=(i-p1>=0?max(f[i-p1],1LL):0)+(i-p2>=0?max(f[i-p2],1LL):0)+(i-p3>=0?max(f[i-p3],1LL):0);
	}
}

int main()
{
	ios::sync_with_stdio(true);
	int tcase;
	scanf("%d",&tcase);
	rep(nowcase,1,tcase) lemon();
	return 0;
}