open Batteries
open Printf
let () =
  let a,b,c = Scanf.scanf "%s %s %s " (fun a b c -> a,b,c) in
  let a = String.to_list a in
  let b = String.to_list b in
  let c = String.to_list c in

  let rec aux a b c next =
    if next = 'a' then 
      (
        match a with
        | [] -> "A"
        | next::tl -> aux tl b c next
      )
    else if next = 'b' then 
      (
        match b with
        | [] -> "B"
        | next::tl -> aux a tl c next
      )
    else if next = 'c' then 
      (
        match c with
        | [] -> "C"
        | next::tl -> aux a b tl next
      )
    else ""
  in
  Printf.printf "%s\n" @@
  aux a b c 'a'
