#include using namespace std; int main() { string s,a; while(cin>>s) { if(s=="#") break; a=s; next_permutation(s.begin(),s.end()); if(s>a) cout<