solve :: String -> String -> String -> Char -> String
solve [] _ _ 'a' = "A"
solve _ [] _ 'b' = "B"
solve _ _ [] 'c' = "C"
solve a b c next
  | next == 'a' = solve (tail a) b c (head a)
  | next == 'b' = solve a (tail b) c (head b)
  | next == 'c' = solve a b (tail c) (head c)

main :: IO ()
main = do
  sa <- getLine
  sb <- getLine
  sc <- getLine
  putStrLn $ solve sa sb sc 'a'