T = int(input()) for _ in range(T): M = int(input()) C = list(map(int, input().split())) A = [] for i in range(9): for _ in range(C[i]): A.append(str(i+1)) for _ in range(9): A.append("0") ans = int("".join(A)) if ans == 0: print(9) continue if ans%M == 0: print(ans) else: ans += M ans -= ans%M print(ans)