# Enter your code here. Read input from STDIN. Print output to STDOUT t=input() store=[] def calc(x): assert x>=0 if x<b: return 1 if not store[x]: res=1 if x>=c: res=calc(x-a)+calc(x-b)+calc(x-c) res=max(res,calc(x-a)+calc(x-b)) store[x]=res return store[x] for _ in range(t): inp=map(int,raw_input().split()) n=inp[0] a,b,c=sorted(inp[1:]) store=[0]*33*c i=1 if n==1: print 0 continue while calc(i)<n: i+=1 #print i, calc(i) print i