module mod_hash_map
  implicit none
  integer, parameter :: key_kind = 8
  integer, parameter :: val_kind = 8
  integer, parameter :: int_max_value = 2147483647
  integer, parameter :: default_initial_capacity = 16
  integer, parameter :: maximum_capacity = lshift(1, 30)
  real, parameter :: default_load_factor = 0.75

  type t_entry
    integer(key_kind) :: key
    integer(val_kind) :: val
    type(t_entry), pointer :: next => null()
    integer :: hash
  end type

  type t_entry_ptr
    type(t_entry), pointer :: ref => null()
  end type

  type t_hash_map
    type(t_entry_ptr), private, allocatable :: table(:)
    integer :: size = 0
    integer, private :: threshold = int(default_initial_capacity * default_load_factor)
    real, private :: load_factor = default_load_factor
  contains
    procedure :: is_empty => is_empty
    procedure :: is_not_empty => is_not_empty
    procedure :: put => put
    procedure :: get => get
    procedure :: get_or_default => get_or_default
    procedure :: remove => remove
    procedure :: clear => clear
    procedure :: contains_key => contains_key
    procedure :: contains_val => contains_val
  end type

  interface hash_map
    module procedure :: newhm0, newhm1, newhm2
  end interface
  
  interface urshift
    module procedure :: urshift0, urshift1
  end interface urshift

contains

  function new_entry(key, val, h) result(res)
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    integer, intent(in) :: h
    type(t_entry), pointer :: res
    allocate(res)
    res%key = key
    res%val = val
    res%hash = h
  end

  integer function hash_code(key) result(res)
    integer(key_kind), intent(in) :: key
    res = merge(xor(key, urshift(key, 32)), key, key_kind == 8)
  end

  type(t_hash_map) function newhm0() result(res)
    allocate(res%table(default_initial_capacity))
  end

  type(t_hash_map) function newhm1(initial_capacity) result(res)
    integer, intent(in) :: initial_capacity
    res = newhm2(initial_capacity, default_load_factor)
  end

  type(t_hash_map) function newhm2(initial_capacity, load_factor) result(res)
    integer, intent(in) :: initial_capacity
    real, intent(in) :: load_factor
    integer :: capacity
    if (initial_capacity < 0) then
      capacity = default_initial_capacity
    else
      capacity = 1
      do while (capacity < min(initial_capacity, maximum_capacity))
        capacity = lshift(capacity, 1)
      end do
    end if

    if (load_factor <= 0 .or. load_factor /= load_factor) then
      res%load_factor = default_load_factor
    else
      res%load_factor = load_factor
    end if

    res%threshold = int(capacity * res%load_factor)
    allocate(res%table(capacity))
  end
  
  integer function urshift0(i, n) result(res)
    integer, intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 31)
    res = merge(i, rshift(ibclr(rshift(i, 1), 31), m - 1), m == 0)
  end

  integer(8) function urshift1(i, n) result(res)
    integer(8), intent(in) :: i
    integer, intent(in) :: n
    integer :: m
    m = and(n, 63)
    res = merge(i, rshift(ibclr(rshift(i, 1), 63), m - 1), m == 0)
  end

  integer function hash(i) result(res)
    integer, intent(in) :: i
    integer :: h
    h = i
    h = xor(h, xor(urshift(h, 20), urshift(h, 12)))
    res = xor(h, xor(urshift(h, 7), urshift(h, 4)))
  end

  integer function index_for(h, length) result(res)
    integer, intent(in) :: h, length
    res = and(h, length - 1) + 1
  end

  logical function is_empty(this) result(res)
    class(t_hash_map), intent(in) :: this
    res = this%size == 0
  end

  logical function is_not_empty(this) result(res)
    class(t_hash_map), intent(in) :: this
    res = this%size /= 0
  end

  integer(val_kind) function get(this, key) result(res)
    class(t_hash_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    integer :: h
    type(t_entry), pointer :: e
    h = hash(hash_code(key))
    e => this%table(index_for(h, size(this%table)))%ref
    do while (associated(e))
      if (e%hash == h .and. e%key == key) then
        res = e%val
        return
      end if
      e => e%next
    end do
    res = 0
  end

  integer(val_kind) function get_or_default(this, key, def) result(res)
    class(t_hash_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: def
    integer :: h
    type(t_entry), pointer :: e
    h = hash(hash_code(key))
    e => this%table(index_for(h, size(this%table)))%ref
    do while (associated(e))
      if (e%hash == h .and. e%key == key) then
        res = e%val
        return
      end if
      e => e%next
    end do
    res = def
  end

  logical function contains_key(this, key) result(res)
    class(t_hash_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    type(t_entry), pointer :: e
    e => get_entry(this, key)
    res = associated(e)
  end

  function get_entry(this, key) result(res)
    class(t_hash_map), intent(in) :: this
    integer(key_kind), intent(in) :: key
    integer :: h
    type(t_entry), pointer :: e
    type(t_entry), pointer :: res
    h = hash(hash_code(key))
    e => this%table(index_for(h, size(this%table)))%ref
    do while (associated(e))
      if (e%hash == h .and. e%key == key) then
        res => e
        return
      end if
      e => e%next
    end do
    res => null()
  end

  subroutine put(this, key, val)
    class(t_hash_map), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    integer :: h, i
    type(t_entry), pointer :: e
    h = hash(hash_code(key))
    i = index_for(h, size(this%table))
    e => this%table(i)%ref
    do while (associated(e))
      if (e%hash == h .and. e%key == key) then
        e%val = val
        return
      end if
      e => e%next
    end do
    call add_entry(this, key, val, h, i)
  end

  subroutine add_entry(this, key, val, h, idx)
    class(t_hash_map), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    integer(val_kind), intent(in) :: val
    integer, intent(in) :: h, idx
    type(t_entry), pointer :: e
    e => this%table(idx)%ref
    this%table(idx)%ref => new_entry(key, val, h)
    this%table(idx)%ref%next => e
    this%size = this%size + 1
    if (this%size >= this%threshold) call resize(this, 2 * size(this%table))
  end

  subroutine resize(this, new_capacity)
    class(t_hash_map), intent(inout) :: this
    integer, intent(in) :: new_capacity
    integer :: capacity, i, j
    type(t_entry), pointer :: e, next
    type(t_entry_ptr) :: table(new_capacity)
    capacity = size(this%table)
    if (capacity == maximum_capacity) then
      this%threshold = int_max_value
      return
    end if

    do j = 1, capacity
      e => this%table(j)%ref
      if (associated(e)) then
        this%table(j)%ref => null()
        do
          next => e%next
          i = index_for(e%hash, new_capacity)
          e%next => table(i)%ref
          table(i)%ref => e
          e => next
          if (.not.associated(e)) exit
        end do
      end if
    end do

    deallocate(this%table)
    allocate(this%table(new_capacity))
    do j = 1, new_capacity
      this%table(j)%ref => table(j)%ref
    end do
    this%threshold = int(new_capacity * this%load_factor)
  end

  subroutine remove(this, key)
    class(t_hash_map), intent(inout) :: this
    integer(key_kind), intent(in) :: key
    integer :: h, i
    type(t_entry), pointer :: e, prev, next
    h = hash(hash_code(key))
    i = index_for(h, size(this%table))
    prev => this%table(i)%ref
    e => prev
    do while (associated(e))
      next => e%next
      if (e%hash == h .and. e%key == key) then
        this%size = this%size - 1
        if (associated(prev, e)) then
          this%table(i)%ref => next
        else
          prev%next => next
        end if
        return
      end if
      prev => e
      e => next
    end do
  end

  subroutine clear(this)
    class(t_hash_map), intent(inout) :: this
    deallocate(this%table)
    allocate(this%table(default_initial_capacity))
    this%size = 0
  end

  logical function contains_val(this, val) result(res)
    class(t_hash_map), intent(in) :: this
    integer(val_kind), intent(in) :: val
    integer :: i
    type(t_entry), pointer :: e
    do i = 1, size(this%table)
      e => this%table(i)%ref
      do while (associated(e))
        if (e%val == val) then
          res = .true.
          return
        end if
        e => e%next
      end do
    end do
    res = .false.
  end
end module mod_hash_map
program pay_to_win
  use mod_hash_map
  implicit none
  integer :: t, i
  integer(8) :: n, a, b, c, d
  read(*,*) t
  do i = 1, t
    read(*,*) n, a, b, c, d
    write(*,'(i0)') solve(n, a, b, c, d)
  end do
contains
  integer(8) function solve(n, a, b, c, d) result(res)
    integer(8), intent(in) :: n, a, b, c, d
    type(t_hash_map) :: memo
    memo = hash_map()
    call memo%put(0_8, 0_8)
    call memo%put(1_8, d)
    call dfs(memo, n, a, b, c, d)
    res = memo%get(n)
  end
  recursive subroutine dfs(memo, n, a, b, c, d)
    integer(8), parameter :: inf = 9223372036854775800_8
    type(t_hash_map), intent(inout) :: memo
    integer(8), intent(in) :: n, a, b, c, d
    integer(8) :: res, i, x, y, z
    if (memo%contains_key(n)) return
    do i = 2, 5
      if (i == 4) cycle
      call dfs(memo, n / i, a, b, c, d)
      call dfs(memo, (n + i - 1) / i, a, b, c, d)
    end do
    res = merge(n * d, inf, n < inf / d)
    do i = 2, 5
      if (i == 4) cycle
      x = n / i
      y = (n + i - 1) / i
      z = merge(a, merge(b, c, i == 3), i == 2)
      res = min(res, memo%get(x) + z + (n - x * i) * d)
      res = min(res, memo%get(y) + z + (y * i - n) * d)
    end do
    call memo%put(n, res)
  end
end program pay_to_win

Measured memory_bytes: 8116