t = int(input()) for i in range(t): m = int(input()) d = list(map(int,input().split())) val = 1 sum_d = sum(d) if sum_d>(int(m/10)+1): val= int(((10**sum_d)-1)/m)-1 #print("-> {} <-".format(val)) while val<=10**18: d_check = [d[k] for k in range(9)] ch = str(val*m) for k in range(len(ch)): if d_check[int(ch[k])-1]>0: d_check[int(ch[k])-1]-=1 if sum(d_check)==0: #print("==>> {} <<==".format(int(ch))) print(int(ch)) break val+=1