module AtCoder

open System
open System.Collections
open System.IO
open System.Linq
open System.Text

module NumericLiteralG =
    let inline FromZero(): ^a = LanguagePrimitives.GenericZero
    let inline FromOne(): ^a = LanguagePrimitives.GenericOne
    let inline FromInt32 n = Seq.sumBy (fun _ -> FromOne()) { 1 .. n }
/// Exが存在することを前提にコードを書く

[<AutoOpen>]
module Ex =
    type long = int64

    type double = float
    /// format対策
    let inline def(): 'a = Unchecked.defaultof<'a>

    let inline long x = int64 x
    let inline double x = float x
    let inline (+=) (x: ^a byref) (y: ^a) = x <- x + y
    let inline (-=) (x: ^a byref) (y: ^a) = x <- x - y

    let inline postIncr (x: ^a byref): ^a =
        x <- x + 1G
        x - 1G

    let inline preIncr (x: ^a byref): ^a =
        x <- x + 1G
        x

    let inline postDecr (x: ^a byref): ^a =
        x <- x - 1G
        x + 1G

    let inline preDecr (x: ^a byref): ^a =
        x <- x - 1G
        x

    let inline incr (x: ^a byref) =
        x <- x + 1G

    let inline decr (x: ^a byref) =
        x <- x - 1G

    type String with
        member __.Reversed() = new string(__.Reverse().ToArray())

[<AutoOpen>]
module Input =
    type ConvertTrait< ^a> =
        { FromStr: string -> ^a }

    module Internal =

        type ConvertBuiltin = ConvertBuiltin
            with
                static member inline ConvertImpl(_: System.Int32): ConvertTrait<System.Int32> = { FromStr = int }
                static member inline ConvertImpl(_: System.Int64): ConvertTrait<System.Int64> = { FromStr = long }
                static member inline ConvertImpl(_: System.Double): ConvertTrait<System.Double> = { FromStr = double }
                static member inline ConvertImpl(_: System.Char): ConvertTrait<System.Char> = { FromStr = char }
                static member inline ConvertImpl(_: System.String): ConvertTrait<System.String> = { FromStr = id }

        let inline getImpl (builtin: ^builtin) (_: ConvertTrait< ^a >) =
            ((^builtin or ^a): (static member ConvertImpl: 'a -> ConvertTrait< ^a >) (def()))

        let inline fromStr (x: string): ^a =
            (getImpl ConvertBuiltin (Unchecked.defaultof<ConvertTrait< ^a >>)).FromStr(x)

        type Input() =



            [<Literal>]
            let size = 5000

            let stream = Console.OpenStandardInput()
            let buffer = Array.zeroCreate size
            let mutable (ptr, bufferSize) = (0, 0)

            let hasNextByte(): bool =
                if ptr < bufferSize then
                    true
                else
                    ptr <- 0
                    bufferSize <- stream.Read(buffer, 0, size)
                    bufferSize > 0


            let readByte(): byte =
                if hasNextByte() then buffer.[postIncr &ptr]
                else failwith "buffer empty"

            let isAscii (c: int): bool = int '!' <= c && c <= int '~'

            let nextChar(): char =
                let mutable b = readByte()
                while (not << isAscii << int) b do
                    b <- readByte()
                char b


            let next(): string =
                let sb = StringBuilder()
                let mutable c = byte <| nextChar()
                while (isAscii << int) c do
                    sb.Append(char c) |> ignore
                    c <- readByte()
                sb.ToString()

            member __.String = next()

        let input = Input()

    let inline get(): ^a = Internal.fromStr (Internal.input.String)
    let inline getString(): string = get()
    let inline getInt(): int = get()

module STL =
    /// 未テスト
    type Deque<'a>(?size: int) =
        let mutable capacity = defaultArg size 100
        let mutable buffer = Array.zeroCreate capacity
        let mutable count = 0
        let mutable offset = 0
        /// TODO: refactor
        let copy (xs: 'a array) (xIdx: int) (ys: 'a array) (yIdx: int) (n: int) =
            for i in 0 .. n - 1 do
                ys.[yIdx + i] <- xs.[xIdx + i]

        member __.Any(): bool = count <> 0

        member private __.Extend() =
            let capacity' = buffer.Length <<< 1
            let buffer' = Array.zeroCreate capacity'
            if offset > capacity - capacity - count then
                let len = buffer.Length - offset
                copy buffer offset buffer' 0 len
                copy buffer 0 buffer' len (count - len)
            else
                copy buffer offset buffer' 0 count
            offset <- 0
            capacity <- capacity'
            buffer <- buffer'

        member private __.GetIndex(idx: int): int =
            if idx >= capacity then failwith "out of range"
            let idx' = idx + offset
            if idx' >= capacity then idx' - capacity
            else idx'

        member __.PushFront(x: 'a) =
            if count = capacity then __.Extend()
            if preDecr &offset < 0 then &offset += buffer.Length
            buffer.[offset] <- x
            incr &count

        member __.PushBack(x: 'a) =
            if count = capacity then __.Extend()
            let id = postIncr &count + offset
            if id >= capacity then buffer.[id - capacity] <- x
            else buffer.[id] <- x

        member __.PopFront(): 'a =
            if not <| __.Any() then failwith "empty"
            decr &count
            let idx = postIncr &offset
            if offset >= capacity then &offset -= capacity
            buffer.[idx]

        member __.PopBuck(): 'a =
            if not <| __.Any() then failwith "empty"
            buffer.[__.GetIndex <| preDecr &count]

let S = getString()
let Q = get()

type Query =
    | One
    | Two of int * char

let QueryN =
    [ for _ in 1 .. Q ->
        let type' = get()
        match type' with
        | 1 -> One
        | _ ->
            let f = get()
            let c = get()
            Two(f, c) ]

let deq = STL.Deque<char>()

S.ToCharArray() |> Array.iter (fun c -> deq.PushBack c)

let mutable isFlip = false

QueryN
|> List.iter (fun q ->
    match q with
    | One -> isFlip <- not isFlip
    | Two(1, c) ->
        if not isFlip then deq.PushFront c
        else deq.PushBack c
    | Two(_, c) ->
        if isFlip then deq.PushFront c
        else deq.PushBack c)

let sb = StringBuilder()

while deq.Any() do
    deq.PopFront()
    |> sb.Append
    |> ignore

if not isFlip then sb.ToString()
else sb.ToString().Reversed()
|> Console.WriteLine


Measured memory_bytes: 37476