from collections import deque

a = deque(list(eval(input())))

b = deque(list(eval(input())))

c = deque(list(eval(input())))



card = a.popleft()



while True:

  if card == "a":

    if not a:

      print("A")

      exit()

    else:

      card = a.popleft()

  elif card == "b":

    if not b:

      print("B")

      exit()

    else:

      card = b.popleft()

  else:

    if not c:

      print("C")

      exit()

    else:

      card = c.popleft()

#print(a,b,c)

S = {x : list(eval(input())) for x in "abc"}

 

s = "a"

while(S[s]):

  s = S[s].pop(0)



print((s.upper()))
from collections import deque

a = deque(list(eval(input())))
b = deque(list(eval(input())))
c = deque(list(eval(input())))
card = a.popleft()
while True:
    if card == "a":
        if not a:
            print("A")
            exit()
        else:
            card = a.popleft()
    elif card == "b":
        if not b:
            print("B")
            exit()
        else:
            card = b.popleft()
    else:
        if not c:
            print("C")
            exit()
        else:
            card = c.popleft()
# print(a,b,c)

S = {x: list(eval(input())) for x in "abc"}
s = "a"
while S[s]:
    s = S[s].pop(0)
print((s.upper()))

-from collections import deque - -a = deque(list(eval(input()))) -b = deque(list(eval(input()))) -c = deque(list(eval(input()))) -card = a.popleft() -while True: -    if card == "a": -        if not a: -            print("A") -            exit() -        else: -            card = a.popleft() -    elif card == "b": -        if not b: -            print("B") -            exit() -        else: -            card = b.popleft() -    else: -        if not c: -            print("C") -            exit() -        else: -            card = c.popleft() -# print(a,b,c) +S = {x: list(eval(input())) for x in "abc"} +s = "a" +while S[s]: +    s = S[s].pop(0) +print((s.upper()))