T = int(input()) for _ in range(T): M = int(input()) D = list(map(int, input().split())) s = '' for i in range(len(D)): s += str(i+1)*D[i] s += '0'*9 s = int(s) print(s + M-(s%M))