import sys from collections import deque,defaultdict import itertools import heapq import bisect import math #sys.setrecursionlimit(10 ** 9) input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) ml = lambda: map(str, input().split()) li = lambda: list(mi()) li_st = lambda: list(map(str, input().split())) lli = lambda n: [li() for _ in range(n)] mod = 998244353 T = ii() for i in range(T): M = ii() D = li() s = [] for i in range(9): for j in range(D[i]): s.append(str(i+1)) num = "".join(s) num = int(num) num *= 10**9 m = num % M if m == 0: print(num) else: print(num + M - m)