// Arup Guha
// 11/14/2015
// Solution to 2015 SER D2 Problem: A Classy Problem

import java.util.*;

public class classy {

	public static HashMap<String,Integer> map;
	public static void main(String[] args) {

		// To make our comparisons easier.
		map = new HashMap<String,Integer>();
		map.put("lower", 2);
		map.put("middle", 1);
		map.put("upper", 0);

		// Read in input.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Store results here.
		person[] list = new person[n];
		for (int i=0; i<n; i++) {

			// Get name, stripping colon.
			String name = stdin.next();
			name = name.substring(0, name.length()-1);

			// Store all adjectives.
			String cur ="";
			ArrayList<String> adjectives = new ArrayList<String>();
			while (!cur.equals("class")) {
				if (!cur.equals("")) adjectives.add(cur);
				cur = stdin.next();
			}

			// Create objects.
			list[i] = new person(name, adjectives);
		}

		// Sort it.
		Arrays.sort(list);

		// Output list.
		for (int i=0; i<n; i++)
			System.out.println(list[i].name);

	}
}

class person implements Comparable<person> {

	public String name;
	public ArrayList<Integer> valueList;

	public person(String n, ArrayList<String> modifiers) {

		name = n;

		// Copy these backwards, which is the right order to do our comparisons.
		valueList = new ArrayList<Integer>();
		for (int i=modifiers.size()-1; i>=0; i--)
			valueList.add(classy.map.get(modifiers.get(i)));
	}

	public int compareTo(person other) {

		// Look for a difference and return if we find out.
		for (int i=0; i<this.valueList.size() && i<other.valueList.size(); i++)
			if (this.valueList.get(i) != other.valueList.get(i))
				return this.valueList.get(i) - other.valueList.get(i);

		// Super annoying case where one list is a prefix of another.
		if (this.valueList.size() > other.valueList.size()) {
			int i = other.valueList.size();
			while (i < this.valueList.size()) {
				if (this.valueList.get(i) == 0) return -1;
				if (this.valueList.get(i) == 2) return 1;
				i++;
			}
		}

		// Super annoying case where one list is a prefix of another.
		if (this.valueList.size() < other.valueList.size()) {
			int i = this.valueList.size();
			while (i < other.valueList.size()) {
				if (other.valueList.get(i) == 0) return 1;
				if (other.valueList.get(i) == 2) return -1;
				i++;
			}
		}

		// Ties broken by name.
		return this.name.compareTo(other.name);
	}
}