def r(x):
if P[x]==x:return x
P[x]=r(P[x]);return P[x]
def u(x,y):
x,y=r(x),r(y)
if x!=y:P[x]=y
while 1:
E,P=input().split(),{}
for e in E:P[e[0]],P[e[2]]=e[0],e[2]
for e in E:u(e[0],e[2])
A=sorted([''.join(sorted([y for y in P if r(y)==k]))for k in P if P[k]==k],key=len)
print(A[0],A[-1])
Note that non-ascii characters in the above source code will be escaped (such as \x9f).