t=int(input()) for _ in range(t): n=int(input()) d=list(map(int,input().split())) s='' for i in range(9): s+=str(i+1)*d[i] if s: s=int(s) else: s=1 s*=10**9 s=(s-1)//n*n+n print(s)