/*
 * BY SATHWIKA
 * NOTES:
 * Lambda Expression Sorting
 * 1. Collections.sort() is wrong as it does not have any defined rules such as sorting only first and second letter in a name
 * 2. Sorting in accordance to different rules we need a Comparator with compare() method that takes two parameters of objects (obj1, obj2) and returns:
 * - negative value (-1) if obj1 < obj2
 * - positive value (+1) if obj1 > obj2
 * - zero (0) if obj1 == obj2
 * 3. Using lambda expression in place of comparator object for defining our own sorting in collections which is simple and understandable
 * 
 * Helpful Links:
 * https://www.geeksforgeeks.org/java-lambda-expression-with-collections/#
 * https://www.baeldung.com/java-8-sort-lambda
 */

import java.util.*;

public class sortofsorting {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int n = in.nextInt();
        while (n != 0) {
            ArrayList<String> list = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                String temp = in.next();
                list.add(temp);
            }

            // 1. Wrong approach
            // Collections.sort(list);

            // 2. Comparator Definition
            // Collections.sort(list, new Comparator<>() {
            // @Override
            // public int compare(String one, String two) {
            // if (one.charAt(0) == two.charAt(0)) {
            // return one.charAt(1) - two.charAt(1);
            // }

            // return one.compareTo(two);
            // }
            // });

            // 3. Lambda Expression Sorting
            // Same first letter, return difference of second letter, else string difference
            Collections.sort(list,
                    (String one, String two) -> (one.charAt(0) == two.charAt(0)) ? (one.charAt(1)
                            - two.charAt(1))
                            : one.compareTo(two));

            for (String s : list) {
                System.out.println(s);
            }
            System.out.println();

            n = in.nextInt();
        }
    }
}