#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
char s[11];
int main()
{
int times = 0;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
int n = strlen(s);
sort(s, s+n);
do
{
printf("%s\n", s);
}while(next_permutation(s, s+n));
printf("\n");
}
return 0;
}
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
char s[11];
int main()
{
int times = 0;
int T;
scanf("%d", &T);
while(T--)
{
scanf("%s", s);
int n = strlen(s);
sort(s, s+n);
do
{
printf("%s\n", s);
}while(next_permutation(s, s+n));
printf("\n");
}
return 0;
}