t = read_line.to_i64 t.times do m = read_line.to_i64 d = read_line.split.map &.to_i64 now = 0_i64 d.each_with_index(1) do |num, i| num.times do now = 10_i64 * now + i end end now *= 10_i64**9 added = m - now % m if added % m == 0 added = 0_i64 end ans = now + added puts ans end