let get_line () =
    read_line ()
    |> Str.split (Str.regexp "")

let rec play a b c turn =
    match turn with
    | "a" when a <> [] ->
        play (List.tl a) b c (List.hd a)
    | "b" when b <> [] ->
        play a (List.tl b) c (List.hd b)
    | "c" when c <> [] ->
        play a b (List.tl c) (List.hd c)
    | _ ->
        String.uppercase turn

let () =
    let a = get_line () in
    let b = get_line () in
    let c = get_line () in
    play a b c "a"
    |> print_endline
