import Control.Monad

main = do
    decks <- replicateM 3 getLine
    putStrLn $ game 'a' decks

type Turn = Char
type Deck = String
type Player = String

proceed :: Turn -> [Deck] -> (Turn, [Deck])
proceed 'a' [a, b, c] = (head a, [drop 1 a, b, c])
proceed 'b' [a, b, c] = (head b, [a, drop 1 b, c])
proceed 'c' [a, b, c] = (head c, [a, b, drop 1 c])

game :: Turn -> [Deck] -> Player
game 'a' ["", _, _] = "A"
game 'b' [_, "", _] = "B"
game 'c' [_, _, ""] = "C"
game turn decks = game $% proceed turn decks

($%) :: (a -> b -> c) -> (a, b) -> c
f $% t = f (fst t) (snd t)