 #include <stdio.h>
 #include <stdlib.h>
  void swap(char a[], char b[]){
      char tmp;
      for(int i=0; i<8; i++){
          tmp = a[i],a[i] = b[i], b[i] = tmp;
      }
  }

  void compare2(char c[],char d[]){
  	for(int i=2; i<8; i++){
          if(c[i]-d[i]<0){
          	swap(c,d);
            return;
          }
          else return;
  	}   
  }
  
   int order(char c){
      if(c == 'x') return -1;  
      return (int)c;
  }

  void BubleSort(char a[][8], int n){
      for(int i=0; i<n-1; i++){
          for(int j=1; j<n-i; j++){
              if(order(a[j][1]) == order(a[j-1][1])){
                  compare2(a[j], a[j-1]);
              }
              else if(order(a[j][1]) - order(a[j-1][1]) < 0){
                  swap(a[j], a[j-1]);
              }
          }
      }
  }
 

  int main(void) {
  	  int n;
      scanf("%d" ,&n);
      char (*v)[8] = malloc(sizeof(char[8])*n);
      
      for(int j=0; j<n; j++){
        
      	  scanf("%s" ,v[j]);
      }
      BubleSort(v,n);
      for(int i=0; i<n; i++){
          printf("%s\n", v[i]);
      }
      free(v);
      return 0;
  }