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) s+=(-s)%m print(s)