def solve(): M = int(input()) d = [0] + list(map(int, input().split())) X_str = '' for i in range(1, 10): X_str += str(i) * d[i] X = int(X_str + ('0' * (18 - len(X_str)))) X += (M - X % M) % M return X #================================================== import sys input=sys.stdin.readline write=sys.stdout.write T = int(input()) write("\n".join(map(str, [solve() for t in range(T)])))