#lexicographic permutations
def nextt(ls):
n=len(ls)
i=n-2
while ls[i]>ls[i+1]:
i=i-1
#print(i)
if i<0: data-blogger-escaped-for="for" data-blogger-escaped-i="i" data-blogger-escaped-ii="ii" data-blogger-escaped-in="in" data-blogger-escaped-j="i+1" data-blogger-escaped-ls="ls" data-blogger-escaped-mx="0" data-blogger-escaped-n="n" data-blogger-escaped-print="print" data-blogger-escaped-range="range" data-blogger-escaped-return="return">ls[i],ls[ii],ls[i],mx,j)
if ls[ii]>ls[i]:
mx=max(mx,ii)
#print(i,mx)
ls[i],ls[mx]=ls[mx],ls[i]
#print(ls)
#t=input()
nw=ls[:i+1]
#print(nw)
rw=ls[i+1:]
#print(rw)
rw.reverse()
ls=nw+rw
print(ls)
nextt(ls)
num=list(map(int,list(input())))
nextt(num)
def nextt(ls):
n=len(ls)
i=n-2
while ls[i]>ls[i+1]:
i=i-1
#print(i)
if i<0: data-blogger-escaped-for="for" data-blogger-escaped-i="i" data-blogger-escaped-ii="ii" data-blogger-escaped-in="in" data-blogger-escaped-j="i+1" data-blogger-escaped-ls="ls" data-blogger-escaped-mx="0" data-blogger-escaped-n="n" data-blogger-escaped-print="print" data-blogger-escaped-range="range" data-blogger-escaped-return="return">ls[i],ls[ii],ls[i],mx,j)
if ls[ii]>ls[i]:
mx=max(mx,ii)
#print(i,mx)
ls[i],ls[mx]=ls[mx],ls[i]
#print(ls)
#t=input()
nw=ls[:i+1]
#print(nw)
rw=ls[i+1:]
#print(rw)
rw.reverse()
ls=nw+rw
print(ls)
nextt(ls)
num=list(map(int,list(input())))
nextt(num)