// Arup Guha
// 12/3/2016
// Written to illustrate custom sorting by implementing the Comparable interface.
import java.util.*;

public class customsort {

	public static void main(String[] args) {

		// Read in number of names.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Read in the names and create student objects for sorting.
		student[] list = new student[n];
		for (int i=0; i<n; i++)
			list[i] = new student(stdin.next());
			
		// Sort them.
		Arrays.sort(list);

		// And print.
		for (int i=0; i<n; i++)
			System.out.println(list[i]);
	}
}

// This class must implement Comparable as shown.
class student implements Comparable<student> {

	// Stores the data.
	private String name;
	private int len;
	
	// Constructor.
	public student(String n) {
		name = n;
		len = name.length();
	}

	// All classes that implement Comparable must have a compareTo method.
	public int compareTo(student other) {

		// We first break ties by name length.
		if (this.len != other.len)
			return this.len - other.len;

		// For names of equal length we sort by alphabetical order without regard to case.
		return this.name.toLowerCase().compareTo(other.name.toLowerCase());
	}

	public String toString() {
		return name;
	}
}