let st = stdin.ReadLine()
let q = stdin.ReadLine() |> int
let qr = [1..q] |> List.map (fun _ -> stdin.ReadLine().Split())
let rec f c i sf sb =
    if i=q then
        [sf;sb]
        |> List.map (Seq.toList >> List.map string)
        |> fun [x;y] ->
            if c%2=0 then (List.reduceBack (fun item ans -> ans + item) x) + (List.reduce (fun ans item -> ans + item) y)
            else (List.reduceBack (fun item ans -> ans + item) y) + (List.reduce (fun ans item -> ans + item) x)
    elif qr.[i].[0]="1" then f (c+1) (i+1) sf sb
    elif (qr.[i].[1]="1" && c%2=0) || (qr.[i].[1]="2" && c%2=1) then f c (i+1) (sf + qr.[i].[2]) sb
    else f c (i+1) sf (sb + qr.[i].[2])
f 0 0 "" st |> stdout.WriteLine