while 1:s=raw_input();v=s[::4];print' '.join(''.join(sorted(q([reduce(lambda r,e:r|(r&set(e)and set(e)),zip(v,s[2::4])*3,{c})for c in v],key=len)))for q in(min,max))