T = int(input()) for _ in range(T): M = int(input()) D = list(map(int, input().split())) D.append(18 - sum(D)) ans = 0 for i, d in enumerate(D, 1): while d > 0: ans *= 10 ans += (i % 10) d -= 1 print((ans + M - 1) // M * M)