1. DFS
def dfs(count):
global answer
if not count:
temp = int(''.join(values))
if answer < temp:
answer = temp
return
for i in range(length):
for j in range(i + 1, length):
values[i], values[j] = values[j], values[i]
k = ''.join(values)
if (k, count - 1) not in visited:
visited.append((k, count - 1))
dfs(count - 1)
values[i], values[j] = values[j], values[i]
for t in range(1, int(input()) + 1):
answer = -1
value, change = input().split()
values = list(value)
change = int(change)
length = len(values)
visited = []
dfs(change)
print(f'#{t} {answer}')