#include <iostream>
#include <string>
using namespace std;

//input line, and its length
char line[10000];
int len;

//The question VERY BAD that it requires 'B' > 'a'. TMD.
//return 1 if a > b
bool greater_than( char a, char b ){
  char upper_a = a;
  if( a >= 'a' ){
    upper_a = a - 'a' + 'A';
  }
  char upper_b = b;
  if( b >= 'a' ){
    upper_b = b - 'a' + 'A';
  }
  if( upper_a > upper_b ){
    return 1;
  }
  if( upper_a == upper_b && a > b ){
    return 1;
  }
  return 0;
}

//sort characters in the input line 
void sort(){
  for( int i=0; i<len; i++ ){
    for( int j=i+1; j<len; j++ ){
      if( greater_than( line[i], line[j] ) ){
        char temp = line[i];
        line[i] = line[j];
        line[j] = temp;
      }
    }
  }
}

//it print all possible permutations of line[now..len-1]
void print( int now ){
  if( now==len-1 ){
    cout << line << endl;
    return;
  }
  //the case for line[now] remains unchanged
  print( now+1 );
  char last = line[now];
  
  //test the cases for using other characters as line[now]
  for( int i=now+1; i<len; i++ ){
    if( greater_than( line[i], last ) ){ 
      //using other character
      //(1) move line[i] to line[now]
      char temp = line[i];
      for( int j=i; j>now; j-- ){
        line[j] = line[j-1];
      }
      line[now] = temp;

      //(2) make recurrsive call
      print( now+1 );

      //(3) move line[now] back to line[i]
      for( int j=now; j<i; j++ ){
        line[j] = line[j+1];
      }
      line[i] = temp;

      //(4) reset last
      last = line[i];
    }
  }
}

int main(){
  int no_case;
  cin >> no_case >> ws;
  for( int i=0; i<no_case; i++ ){  
    cin >> line;
    len = strlen( line );
    sort();
    print( 0 );
  }
  return 1;
}
  


