def Map(): return list(map(int,input().split())) T = int(input()) for _ in range(T): M = int(input()) D = Map() D = [0] + D S = [] for d in range(1,10): for i in range(D[d]): S.append(str(d)) s = ''.join(S) s = int(s) s *= 10**9 r = (M-s) % M s += r print(s)