let split_string ?(pattern="") = Str.split @@ Str.regexp pattern
let s = read_line () |> split_string
let q = Scanf.sscanf (read_line ()) "%d" @@ fun q -> q
let qq = Array.init q @@ fun _ -> read_line () |> split_string ~pattern:" "

let (s, t) = Array.fold_left (fun (s, t) -> function
  | "1" :: [] -> (t, s)
  | "2" :: f :: c :: [] -> if f = "1" then (c :: s, t) else (s, c :: t)
  | _ -> failwith ""
) (s, []) qq

let () =
  List.iter print_string s; 
  List.iter print_string (List.rev t); 
  print_newline ()