T = int(input()) ans = [] for _ in range(T): M = int(input()) D = list(map(int, input().split())) s = '' for i in range(len(D)): s += str(i+1)*D[i] s += '9'*(18 - len(s)) s = int(s) ans.append(s - s%M) print(*ans, sep='\n')