# Read the number of test cases t = int(input()) N = 1000000000 for _ in range(t): # Read M for each test case M = int(input()) ans = 0 # Process the nine digits d = list(map(int, input().split())) for i in range(1, 10): for j in range(0,d[(i-1)]): ans *= 10 ans += i # Calculate and print the final answer ans *= N ans += M - (ans % M) print(ans)