t = int(input()) for i in range(t): m = int(input()) d = list(map(int, input().split())) k = 0 #少なくとも a="" for l in range(1,10): a+=str(l)*d[l-1] a = int(a) k = a//m-1 while True: k += 1 x = k*m x_ = str(x) for i in range(1, 10): if x_.count(str(i)) < d[i-1]: break else: print(x) break