/*
求有任意n个字母组成的所有长度为n的字符串
字符之间的大小关系是A < a < B < b < C < c ...
有求输出按照由小到大的顺序
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX_L 13
using namespace std;
char input[MAX_L + 2];
char res[MAX_L + 2];
bool v[MAX_L + 2];
bool compare(char c1, char c2)
{
if((c1 >= 'A' && c1 <= 'Z' && c2 >= 'A' && c2 <= 'Z') ||
(c1 >= 'a' && c1 <= 'z' && c2 >= 'a' && c2 <= 'z'))
{
if(c1 <= c2) return true;
else return false;
}
else
{
if(c1 >= 'A' && c1 <= 'Z')
{
int d1 = c1 - 'A';
int d2 = c2 - 'a';
if(d1 <= d2) return true;
else return false;
}
else
{
int d1 = c1 - 'a';
int d2 = c2 - 'A';
if(d1 < d2) return true;
else return false;
}
}
}
int strLen;
void dfs(int curPos, bool v[])
{
if(curPos == strLen)
{
res[strLen] = '/0';
printf("%s/n", res);
return;
}
bool v2[MAX_L + 1];
//memcpy(v, v2, sizeof(v));
for(int t = 0; t <= MAX_L; t++) v2[t] = v[t];
int c = strLen - curPos, p = 0;
char lastChar = '.';
while(c > 0)
{
while(v[p] || input[p] == lastChar)
{
if(input[p] == lastChar) c--;
if(c == 0) break;
p++;
if(p == strLen) p = 0;
}
if(c == 0) break;
lastChar = input[p];
c--;
v2[p] = true;
res[curPos] = input[p];
dfs(curPos + 1, v2);
v2[p] = false;
p++; if(p == strLen) p = 0;
}
}
int main()
{
int cNum, i, j;
scanf("%d", &cNum);
for(i = 1; i <= cNum; i++)
{
scanf("%s", input);
strLen = strlen(input);
sort(input, input + strLen, compare);
memset(v, 0, sizeof(v));
dfs(0, v);
}
return 0;
}