https://www.acmicpc.net/problem/6443
import sys
from collections import defaultdict
def go(cnt):
if cnt == len(st):
print("".join(ans))
return
for i in range(ord('a'), ord('z') + 1):
curr_alpha = chr(i)
if alpha_dict[curr_alpha]:
alpha_dict[curr_alpha] -= 1
ans.append(curr_alpha)
go(cnt + 1)
alpha_dict[curr_alpha] += 1
ans.pop()
n = int(sys.stdin.readline())
for _ in range(n):
alpha_dict = defaultdict(int)
ans = []
st = list(map(str, sys.stdin.readline().strip()))
for s in st:
alpha_dict[s] += 1
go(0)