#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
ios::sync_with_stdio(false), cin.tie(nullptr);
int t;
cin >> t;
bool first_line = true;
while(t--){
string s;
cin >> s;
sort(s.begin(), s.end());
if(!first_line){
cout << '\n' << '\n';
}
first_line = false;
cout << s;
while(next_permutation(s.begin(), s.end())){
cout << '\n' << s;
}
}
string s;
s = "ABCDEFGHI";
sort(s.begin(), s.end());
first_line = false;
cout << s << '\n';
while(next_permutation(s.begin(), s.end())){
cout << s << '\n';
}
cout << '\n';
}