def solve(): M = int(input()) d = list(map(int, input().split())) ans = "" for i, d in enumerate(d, start=1): ans += str(i) * d ans = int(ans) * 10**9 ans += (M - ans) % M print(ans) T = int(input()) for _ in range(T): solve()