
![업로드중..]()
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
for(int i=1; i<=T; i++) {
String word = br.readLine();
char[] array = word.toCharArray();
nextPermutation(array);
System.out.println(array);
}
}
private static void nextPermutation(char[] array) {
int i = array.length-2;
while(i>=0 && array[i] >= array[i+1]) {
i--;
}
if(i == -1) return;
int j = array.length-1;
while(array[i] >= array[j]) {
j--;
}
char temp = array[i];
array[i] = array[j];
array[j] = temp;
int start=i+1, end=array.length-1;
while(start < end) {
temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}