def solve(M,D): ans = 0 for i,d in enumerate(D,1): while d: ans += i ans *= 10 d -= 1 ans *= 10**8 add = (M - ans%M) % M ans += add assert ans % M == 0 assert ans <= 10**18 print(ans) T = int(input()) for _ in range(T): M = int(input()) D = list(map(int,input().split())) solve(M,D)