program yukicoder_2562
  use, intrinsic :: iso_fortran_env
  implicit none
  integer(int32) :: t
  integer(int32) :: i
  read(input_unit, *) t
  do i = 1, t
     call solve()
  end do
contains
  impure subroutine solve()
    integer(int32) :: m
    integer(int32) :: ds(9)
    integer(int64) :: x
    integer(int32) :: i, j
    read(input_unit, *) m
    read(input_unit, *) ds(:)
    x = 0_int64
    do i = 1, size(ds)
       do j = 1, ds(i)
          x = x * 10_int64 + int(i, int64)
       end do
    end do
    x = x * 10_int64 ** 9
    ! write(error_unit, *) x, mod(x, m)
    x = x + (m - mod(x, m))
    ! write(error_unit, *) mod(x, m)
    write(output_unit, '(i0)') x
  end subroutine solve
end program yukicoder_2562