   program agc044a
      implicit none
      integer(8)::t
      integer(8),allocatable,dimension(:)::n,a,b,c,d
      integer(8)::bef(3)
      integer(8)::i,j,num,div_mark

         read *, t
         allocate( n(t), a(t), b(t), c(t), d(t) )
         do i=1,t
            read *, n(i), a(i), b(i), c(i), d(i)
         end do

         num=1_8; div_mark=0_8
         do i=1,t
            bef(1)=a(i)
            bef(2)=b(i)
            bef(3)=c(i)
            print *, using_coins( i, num, bef, div_mark )
         end do
         stop


   contains
      recursive function using_coins( t, num, bef, div_mark ) result(coins)
         implicit none
         integer(8),intent(in)::t
         integer(8),intent(inout)::num,div_mark
         integer(8),dimension(:),intent(in)::bef(:)
         integer(8)::coins
         integer(8)::i,j,k
         integer(8)::dam_ans=10**9

            if (n(t)==1) then
               coins = d(t)
               return
            end if

            if (num==n(t)) then
               coins = coins + d(t)
               return
            end if


            if (num<n(t)) then
               num = num*2
               div_mark = 1
               coins = using_coins( t, num, bef, div_mark ) + a(t)
            end if

            if (num<n(t)) then
               num = num*3
               div_mark = 2
               coins = using_coins( t, num, bef, div_mark ) + b(t)
            end if

            if (num<n(t)) then
               num = num*5
               div_mark = 5
               coins = using_coins( t, num, bef, div_mark ) + c(t)
            end if


            coins = coins + min( coins-bef(div_mark) + d(t)*abs(n(t)-num), coins + d(t)*abs(n(t)-num) )

            if ( coins < dam_ans ) then
               dam_ans = coins
               return
            end if

            coins = dam_ans
            return

      end function using_coins

   end program agc044a