def allChars(a):
return sum([ord(_) for _ in a])
#======================
things=raw_input()#'stol stul dverj krovatj kompjuter'
corrupts=raw_input()#'vatjkrotulsredvjlostkompterju'
print 'Initial:'
print things
print corrupts,'\n-----------------------------\n'
thingArr=things.split(' ')
thingLen=list(set([len(_) for _ in thingArr]))
thingDict={allChars(_):[0,_] for _ in thingArr}
i=0;j=0
while i<len(corrupts):
for l in thingLen:
if allChars(corrupts[i:i+l]) in thingDict:
thingDict[allChars(corrupts[i:i+l])][0]=j
i+=l;j+=1
break
thingResult=[_[1] for _ in sorted(thingDict.values())]
i=0;corruptResult=[]
for l in [len(_) for _ in thingResult]:
corruptResult.append(corrupts[i:i+l])
i+=l
print 'Final:'
print ' '.join(thingResult)
print ' '.join(corruptResult)
CmRlZiBhbGxDaGFycyhhKToKICByZXR1cm4gc3VtKFtvcmQoXykgZm9yIF8gaW4gYV0pCiM9PT09PT09PT09PT09PT09PT09PT09Cgp0aGluZ3M9cmF3X2lucHV0KCkjJ3N0b2wgc3R1bCBkdmVyaiBrcm92YXRqIGtvbXBqdXRlcicKY29ycnVwdHM9cmF3X2lucHV0KCkjJ3ZhdGprcm90dWxzcmVkdmpsb3N0a29tcHRlcmp1JwoKcHJpbnQgJ0luaXRpYWw6JwpwcmludCB0aGluZ3MKcHJpbnQgY29ycnVwdHMsJ1xuLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS1cbicKCnRoaW5nQXJyPXRoaW5ncy5zcGxpdCgnICcpCnRoaW5nTGVuPWxpc3Qoc2V0KFtsZW4oXykgZm9yIF8gaW4gdGhpbmdBcnJdKSkKdGhpbmdEaWN0PXthbGxDaGFycyhfKTpbMCxfXSBmb3IgXyBpbiB0aGluZ0Fycn0KCmk9MDtqPTAKd2hpbGUgaTxsZW4oY29ycnVwdHMpOgogIGZvciBsIGluIHRoaW5nTGVuOgogICAgaWYgYWxsQ2hhcnMoY29ycnVwdHNbaTppK2xdKSBpbiB0aGluZ0RpY3Q6CiAgICAgIHRo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