n = int(input())
x = int(input())
y = int(input())
num_str = str(n)
x_pos = num_str.find(str(x))
y_pos = num_str.find(str(y))
if x_pos == -1 or y_pos == -1:
print(0)
elif x_pos < y_pos:
print(x)
elif y_pos < x_pos:
print(y)
else:
print(0)
n = int(input())
x = input().strip()
y = input().strip()
n_str = str(n)
pos_x = None
pos_y = None
for i, char in enumerate(n_str):
if char == x and pos_x is None:
pos_x = i
if char == y and pos_y is None:
pos_y = i
if pos_x is not None and pos_y is not None:
print(x if pos_x < pos_y else y)
else:
print(0)
s,x,y=input().split();a=s.find(x);b=s.find(y);print((x if a<b else y) if a!=-1 and b!=-1 else 0)