import itertools while True: try: s = input().split() n, words, word, inx = s[0], s[1:-2], s[-2].strip(), int(s[-1]) # permu = list(itertools.permutations(word, len(word))) # bros = [w for w in words if tuple(w) in permu and w!=tuple(word)] bros=[] ...