t = int(input()) for i in range(t): m = int(input()) siz = 0 kari = m while kari > 0: kari = kari//10 siz += 1 d = list(map(int, input().split())) num = [] for i in range(9): for j in range(d[i]): num.append(i+1) n = 0 for i in range(len(num)): n *= 10 n += num[i] n %= m print(num[i], end="") if n==0: print() else : for i in range(siz): n *= 10 n %= m m -= n print(m)