#include <iostream>
#include <map>
using namespace std;

map<char, int> m;

bool compare(char s1, char s2) {
	return (m[s1] < m[s2]);
}

int main() {
	string s;
	int n, value = 1;
	for (char x = 'A'; x <= 'Z'; x++) {
		m[x] = value;
		value += 2;
	}
	value = 2;
	for (char x = 'a'; x <= 'z'; x++) {
		m[x] = value;
		value += 2;
	}
	
	scanf("%d", &n);
	for (int x = 1; x <= n; x++) {
		cin >> s;
		sort(s.begin(), s.end(), compare);
		do {
			printf("%s\n", s.c_str());
		} while (next_permutation(s.begin(), s.end(), compare));
	}
	return 0;
}
