Y = 10**9 Z = 10**18 T = int(input()) for _ in range(T): M = int(input()) ds = list(map(int, input().split())) X = 0 p = 1000000000 for i in range(9): for j in range(ds[i]): X += p*(i+1) p *= 10 diff = X % M ans = X+M-diff assert ans <= Z assert ans % M == 0 print(ans)