#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int n;
    string str;
    cin >> n;
    while (n--){
        cin >> str;
        sort(str.begin(), str.end());
        do{
            for (char &c : str)  // c++11 new standard:用來走遍所有元素
                cout << c;
            cout << endl;
        } while (next_permutation(str.begin(), str.end()));
        cout << endl;
    }
}