T = int(input()) for _ in range(T): M = int(input()) X = list(map(int, input().split())) all_zero = True for x in X: if x != 0: all_zero = False if all_zero: print(M) continue Y = "" for i in range(9): for j in range(X[i]): Y += str(i + 1) Z = int(Y) * (10 ** 9) r = M - Z % M if r >= M: r -= M print(Z + r)