N=int(input()) for _ in range(N): M=int(input()) A=list(map(int, input().split())) B=[] for i in range(9): for j in range(A[i]): B.append(str(i+1)) if len(B)==0: print(M) else: c=int(''.join(B))*10**9 d=c%M if d!=0: c+=M-d print(c)