program name
    use,intrinsic :: iso_fortran_env
    implicit none
    integer(int32):: an,bn,cn,ai,bi,ci
    character(100):: sa,sb,sc
    character(1):: t

    read*, sa
    read*, sb
    read*, sc
    an = len_trim(sa)
    bn = len_trim(sb)
    cn = len_trim(sc)

    ai=1;bi=1;ci=1
    t = 'a'
    
    do
        select case(t)
        case('a')
            if (ai > an) then
                print'(a)', 'A'
                stop
            end if
            t=sa(ai:ai)
            ai=ai+1
        case('b')
            if (bi > bn) then
                print'(a)', 'B'
                stop
            end if
            t=sb(bi:bi)
            bi=bi+1
        case('c')
            if (ci > cn) then
                print'(a)', 'C'
                stop
            end if
            t=sc(ci:ci)
            ci=ci+1
        end select
    end do
end program name