#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
bool cmp(char a,char b)
{
if(a>='A'&&a<='Z'&&b>='A'&&b<='Z')
return a<b;
if(a>='a'&&a<='z'&&b<='z'&&b>='a')
return a<b;
if(a>='a'&&a<='z'&&b>='A'&&b<='Z')
return a<(b+32);
if(a>='A'&&a<='Z'&&b>='a'&&b<='z')
return (a+32)<=b;//体现了A<a这一点;
}
int main()
{
int t;
char s[15];
scanf("%d",&t);
while(t--)
{
scanf("%s",s);
int n=strlen(s);
sort(s,s+n,cmp);
do {printf("%s\n",s);
}while(next_permutation(s,s+n,cmp));
}
return 0;
}