T=int(input()) for _ in range(T): m=int(input()) d=[9]+list(map(int,input().split())) s="" for i in reversed(range(10)): s+=str(i)*d[i] s=int(s) t=(-s)%m s+=t if t!=0 else m print(s)