T = int(input()) for _ in range(T): M = int(input()) d = list(map(int,input().split())) lst = [] for i in range(9): for _ in range(d[i]): lst.append(i+1) for j in range(len(lst)): lst[j] *= 10**(9+j) ans = sum(lst) + 999999999 ans -= ans%M print(ans)