import java.util.*;

public class cups {

	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		
		// Create and read in array of cups.
		cup[] mine = new cup[n];
		for (int i=0; i<n; i++) {
			
			String p1 = stdin.next();
			String p2 = stdin.next();
			
			if (isLowerCaseString(p1))
				mine[i] = new cup(p1, Integer.parseInt(p2));
			else
				mine[i] = new cup(Integer.parseInt(p1), p2);
		}
		
		// Sort list.
		Arrays.sort(mine);
		for (int i=0; i<n; i++)
			System.out.println(mine[i]);
	}
	
	public static boolean isLowerCaseString(String s) {
		for (int i=0; i<s.length(); i++)
			if (s.charAt(i) < 'a' || s.charAt(i) > 'z')
				return false;
		return true;
	}
}

class cup implements Comparable<cup> {

	public int diameter;
	public String color;
	
	public cup(int myd, String myc) {
		diameter = myd;
		color = myc;
	}
	
	public cup(String myc, int myr) {
		diameter = 2*myr;
		color = myc;
	}
	
	// Returns negative integer of this < other
	// returns 0 if they are equal
	// returns a positive integer if this > other
	public int compareTo(cup other) {
	
		if (this.diameter != other.diameter)
			return this.diameter - other.diameter;
			
		// Breaking ties by color.
		return this.color.compareTo(other.color);
	}
	
	// Returns the string representation of the object.
	public String toString() {
		return color+" with diameter "+diameter;
	}
}