/*
Subject: #146 ID Codes
e-mail: morcavon@gmail.com
Homepage: http://www.morcavon.com
Building: 25/05/2005 ~ 25/05/2005
Last Update: 25/05/2005
*/
/*
<PREDICATE>
Input consisting of up to 50 characters drwan from 26 lower case letters.
<INPUT>
abcd...
.....
# #: termination char
<OUTPUT>
successor code
(or)
No Successor
*/
#if 1 // 성실 버전=_=
#include <iostream>
#include <cstring>
using namespace std;
#define SWAP(x, y) ((t) = (x), (x) = (y), (y) = (t))
int main()
{
int i, j, k, n;
char input[51];
char t;
while (cin >> input && input[0] != '#') {
n = strlen(input);
for (k = n - 1; k > 0; k--) {
// 마지막 문자보다 작은 문자를 찾아서 스왑
for (i = k - 1; i >= 0; i--) {
if (input[k] > input[i]) {
SWAP(input[k], input[i]);
break;
}
}
if (i < 0)
continue;
else {
// 찾은 문자 바로 다음부터의 문자열을 가지고 가장 작은 문자열을 만든다.
// (정렬하면 될 듯..=_=)
for (i += 1; i < n - 1; i++) {
for (j = i + 1; j < n; j++)
if (input[i] > input[j])
SWAP(input[i], input[j]);
}
cout << input << endl;
break;
}
}
if (k <= 0)
cout << "No Successor\n";
} // end of while()
return 0;
}
#endif
#if 0 // 얍삽버전=_=;;;
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
string input;
while (cin >> input && input != "#") {
if (next_permutation(input.begin(), input.end()))
cout << input << endl;
else
cout << "No Successor\n";
}
return 0;
}
#endif