// Arup Guha
// 2/19/2016
// Written in Junior Knights - an example of a custom sort.

import java.util.*;

public class person implements Comparable<person> {

	// The two components of a person.
	private String firstName;
	private String lastName;
	private int year;

	public person(String first, String last, int myyear) {
		firstName = first;
		lastName = last;
		year = myyear;
	}

	public int compareTo(person other) {

		// We list older people first.
		if (this.year != other.year)
			return this.year - other.year;

		// If last names aren't equal, just compare these.
		if (!this.lastName.equals(other.lastName))
			return this.lastName.compareTo(other.lastName);

		// If we get here, the last names are the same. Break tie by first name.
		return this.firstName.compareTo(other.firstName);
	}

	// Returns a string representation of a person object - used by print/println.
	public String toString() {
		return firstName+" "+lastName+" born on "+year;
	}

	public static void main(String[] args) {

		// Get the number of people.
		Scanner stdin = new Scanner(System.in);
		System.out.println("How many names do you want to enter?");
		int n = stdin.nextInt();

		// Make the array of person.
		person[] list = new person[n];
		for (int i=0; i<n; i++) {
			System.out.println("Please enter the first and last name of person "+(i+1));
			String first = stdin.next();
			String last = stdin.next();
			System.out.println("What year were you born?");
			int year = stdin.nextInt();
			list[i] = new person(first, last, year);
		}

		// Sort and output the list.
		Arrays.sort(list);
		for (int i=0; i<n; i++)
			System.out.println(list[i]);
	}

}