T = int(input()) for _ in range(T): M = int(input()) D = list(map(int,input().split())) ans = 0 for i in range(1,10): for j in range(D[i-1]): ans = 10*ans + i print(ans + 10**9*((-ans)%M))