import sys for s in sys.argv[1:]:a=s.split();print(" ".join(sorted(a[1:],key=lambda t:[*map(a[0].find,t)])))