open System
open System.Collections
open System.Collections.Generic

[<AutoOpen>]
module Cin =
    let read f = stdin.ReadLine() |> f
    let reada f = stdin.ReadLine().Split() |> Array.map f
    let readChars() = read string |> Seq.toArray
    let readInts() = readChars() |> Array.map (fun x -> Convert.ToInt32(x.ToString()))

[<AutoOpen>]
module Cout =
    let writer = new IO.StreamWriter(new IO.BufferedStream(Console.OpenStandardOutput()))
    let print (s: string) = writer.Write s
    let println (s: string) = writer.WriteLine s
    let inline puts (s: ^a) = string s |> println

// -----------------------------------------------------------------------------------------------------

type Deque<'a> =
    { mutable front: 'a list
      mutable rBack: 'a list }

    interface IEnumerable<'a> with
        member self.GetEnumerator() =
            let dst =
                seq {
                    yield! self.front
                    yield! List.rev self.rBack
                }
            dst.GetEnumerator()

    interface IEnumerable with
        member self.GetEnumerator() = (self :> IEnumerable<'a>).GetEnumerator() :> IEnumerator

[<RequireQualifiedAccess>]
[<CompilationRepresentation(CompilationRepresentationFlags.ModuleSuffix)>]
module Deque =

    /// O(1)
    let inline init() =
        { Deque.front = []
          rBack = [] }

    /// O(n)
    let inline build (src: 'a seq) =
        let src = src |> Seq.splitInto 2

        let front =
            src
            |> Seq.head
            |> List.ofSeq

        let rBack =
            src
            |> Seq.tryItem 1
            |> function
            | Some(a) -> List.ofSeq a
            | None -> []
            |> List.rev

        { Deque.front = front
          rBack = rBack }

    let inline cons x deque =
        let front = x :: deque.front
        deque.front <- front

    let inline conj x deque =
        let rBack = x :: deque.rBack
        deque.rBack <- rBack

    let inline uncons deque =
        match deque.front, deque.rBack with
        | [], [] -> raise <| System.Exception("deque is empty")
        | hd :: tl, _ ->
            deque.front <- tl
            hd
        | _, xs ->
            let xs = xs |> List.rev
            deque.rBack <- List.tail xs
            List.head xs

    let inline unconj deque =
        match deque.front, deque.rBack with
        | [], [] -> raise <| System.Exception("deque is empty")
        | _, hd :: tl ->
            deque.rBack <- tl
            hd
        | xs, _ ->
            let xs = xs |> List.rev
            deque.rBack <- List.tail xs |> List.rev
            List.head xs

    let inline ofSeq deque =
        seq {
            yield! deque.front
            yield! List.rev deque.rBack
        }

type Deque<'a> with

    /// O(1)
    member self.PushFront(x: 'a) = Deque.cons x self

    /// O(1)
    member self.PushBack(x: 'a) = Deque.conj x self

    /// O(1)
    member self.PopFront() = Deque.uncons self

    /// O(1)
    member self.PopBack() = Deque.unconj self

    member self.IsEmpty =
        match self.front, self.rBack with
        | [], [] -> true
        | _ -> false

    member self.Any = not self.IsEmpty

    member self.Length = List.length self.front + List.length self.rBack

// -----------------------------------------------------------------------------------------------------

let main() =
    let strRev (s: string): string =
        s
        |> Seq.rev
        |> Seq.map string
        |> String.concat ""

    let S = read string
    let Q = read int
    let que = Deque.build S
    let mutable front = 1
    for i in 0 .. Q - 1 do
        let q = reada string
        match q.[0] with
        | "1" -> front <- front ^^^ 1
        | _ ->
            let c = q.[2].[0]
            match q.[1] with
            | "1" ->
                if front = 1 then que.PushFront(c) else que.PushBack(c)
            | _ ->
                if front = 1 then que.PushBack(c) else que.PushFront(c)

    let ans = Deque.ofSeq que
    let mutable ans = String.Join("", ans)
    if front = 0 then ans <- strRev ans
    puts ans
    ()

// -----------------------------------------------------------------------------------------------------
main()
writer.Dispose()


Measured memory_bytes: 52352