/*   @JUDGE_ID:   1705PZ   195   C */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int sort_function(const void *a,const void *b)
{
	return (*(char *)a - *(char *)b);
}

int main(){
int i,j,k,n,length,count;
char temp,string[100];
scanf("%d",&n);
for(i = 0;i < n;i++){
	scanf("%s",string);
	length = strlen(string);
	qsort((void *)string,length,sizeof(char),sort_function);
	while(1){
		printf("%s\n",string);
		count = 0;
		for(j = 0;j < length - 1;j++)
			if(string[j] < string[j + 1])
				count++;
		if(count == 0)
			break;
		for(j = length - 1;string[j] <= string[j - 1];j--);
		for(k = length - 1;k >= j;k--){
			if(string[j - 1] < string[k]){
				temp = string[j - 1];
				string[j - 1] = string[k];
				string[k] = temp;
				break;
				}
			}
		qsort((void *)(string + j),length - j,sizeof(char),sort_function);
		}
	}
return 0;
}
@END_OF_SOURCE_CODE

