public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String line = "";
        HashMap<Integer, Character> hm2 = new HashMap<Integer, Character>();
        int con = 1;
        int[] arr = new int[200];
        for (int i = 0; i < 26; i++) {
            arr[i+'A'] = con;
            hm2.put(con++, (char)('A'+i));
            arr[(char)('A'+i+32)] = con;
            hm2.put(con++, ((char)('A'+i+32)));
        }
        int times = Integer.parseInt(bf.readLine());
        for (int i = 0; i < times; i++) {
            line = bf.readLine();
            char[] lets = line.toCharArray();
            int[] letn = new int[lets.length];
            for (int j = 0; j < lets.length; j++) 
                letn[j] = arr[(int)lets[j]];
            Arrays.sort(letn);
            sb.append(printArr(letn, hm2)).append("\n");
            while(next_permutation(letn))
                sb.append(printArr(letn, hm2)).append("\n");
        }
        System.out.print(sb);
    }