character(100) a,b,c
character x
read*,a,b,c
x="a"
do
	if(x=="a")then
		if(trim(a)=="")then
        	print"(A)","A"
        	exit
        else
        	x=a(1:1)
            a=a(2:)
       	endif
    else if(x=="b")then
			if(trim(b)=="")then
        		print"(A)","B"
        		exit
        	else
        		x=b(1:1)
            	a=b(2:)
       		endif
	else
		if(trim(c)=="")then
        	print"(A)","C"
        	exit
        else
        	x=c(1:1)
            c=c(2:)
       	endif
    endif
end do
end