T = int(input()) for _ in range(T): M = int(input()) D = list(map(int, input().split())) S = [] for i, d in enumerate(D): for _ in range(d): S.append(str(i + 1)) x = int("".join(S)) * 10 ** 9 y = -x % M print(x + y)