program ABC045B
    implicit none
    character(100),dimension(3)::S
    character(3)::R="ABC"
    character(1)::out
    integer(8),dimension(3)::L
    integer(8)::i,a,b,c,flg
    read(5,*)(S(i),i=1,3)
    a=1
    b=1
    c=1
    flg=0!1:A, 2:B, 3:C
    L(1)=len_trim(S(1))
    L(2)=len_trim(S(2))
    L(3)=len_trim(S(3))
    out=S(1)(a:a)
    a=a+1
    do
        if(out=='a')then
            if(a==L(1)+1)then
                flg=1
                exit
            else
                out=S(1)(a:a)
                a=a+1
            end if
        else if(out=='b')then
            if(b==L(2)+1)then
                flg=2
                exit
            else
                out=S(2)(b:b)
                b=b+1
            end if
        else if(out=='c')then
            if(c==L(3)+1)then
                flg=3
                exit
            else
                out=S(3)(c:c)
                c=c+1
            end if
        end if
    end do

    print'(A)',R(flg:flg)
end program ABC045B