for _ in range(int(input())): m = int(input()) d = list(map(int, input().split())) ans = "" for i in range(9): ans += str(i + 1) * d[i] ans = int(ans) * (10 ** 9) ans += -ans % m assert(ans % m == 0) print(ans)