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] num += "0" * 9 int_num = int(num) m = int_num % M print(int_num + M - m)