program name
    use,intrinsic :: iso_fortran_env
    implicit none
    character(100000)::s
    character(:),allocatable:: ts, bs
    character(1):: top(200000), bottom(200000)
    character(1),allocatable:: sar(:)
    character(5):: query_input
    integer(int32):: i,q,ti,bi
    logical:: flip, insert_top

    read*, s
    read*, q
    flip = .false.
    ti=1
    bi=1

    do i=1,q
        read'(a)', query_input
        if (query_input(1:1) == '1') then
            flip = .not. flip
            cycle
        end if

        insert_top = .false.
        if (query_input(3:3) == '1') insert_top = .true.
        if (flip .neqv. insert_top) then
            top(ti) = query_input(5:5)
            ti=ti+1
        else
            bottom(bi) = query_input(5:5)
            bi=bi+1
        end if
    end do
    ti=ti-1
    bi=bi-1
    allocate(character(ti):: ts)
    allocate(character(bi):: bs)

    if (.not. flip) then
        do i=1,ti
            ts(i:i) = top(ti+1-i)
        end do
        do i=1,bi
            bs(i:i) = bottom(i)
        end do
        print'(a)', ts // trim(s) // bs
    else
        allocate(sar(len_trim(s)))
        do i=1,len_trim(s)
            sar(i) = s(i:i)
        end do
        do i=1,len_trim(s)
            s(i:i) = sar(len_trim(s)+1-i)
        end do
        do i=1,ti
            ts(i:i) = top(i)
        end do
        do i=1,bi
            bs(i:i) = bottom(bi+1-i)
        end do
        print'(a)', bs // trim(s) // ts
    end if
end program name