#緑以下F

for _ in range(int(input())):
    M = int(input())
    D = [0] + list(map(int,input().split()))
    N = 0
    for d in range(10):
        for i in range(D[d]):
            N = N*10+d
    N *= 10**9
    if N%M != 0: N += M-(N%M)
    print(N)