n = int(input()) from collections import Counter def check_ans(ans, d) -> bool: c = Counter(str(ans)) for i, count in enumerate(d, 1): if c[str(i)] < count: return False return True for _ in range(n): M = int(input()) d = list(map(int, input().split())) ans = "" for i, count in enumerate(d, 1): ans += str(i) * count while len(ans) < 18: # 18桁にする ans += "9" ans = int(ans) remainder = ans % M ans -= remainder if check_ans(ans, d) == False: assert False print(ans)