#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
char s[20];
cin>>s;
int len=strlen(s);
sort(s,s+len);
do
{
cout<<s<<endl;
}while(next_permutation(s,s+len));
cout<<endl;
}
return 0;
}