T = int(input()) for i in range(T): M = int(input()) D = list(map(int, input().split())) num = "" for i in range(9): num += str(i + 1) * D[i] int_num = int(num) m = int_num % M m = M - m l = len(str(m)) num += "0" * l int_num = int(num) print(int_num + m)