t=int(input()) for _ in range(t): m=int(input()) d=[0]+list(map(int,input().split())) t=0 for i in range(10): for j in range(d[i]): t*=10 t+=i t*=10**9 t=((t+m-1)//m)*m print(t)