import java.io.*;

public class blastamon{
	
	public static int n;
	public static int hp[]=new int[10000];
	public static String name[]=new String[10000];
	
	public static void main(String args[]) throws IOException{
		BufferedReader infile=new BufferedReader(new FileReader("blastamon.in"));
		
		do{
		
			n=Integer.parseInt(infile.readLine());
			for (int x=0; x<n;x++){
				name[x]=infile.readLine();
				hp[x]=Integer.parseInt(infile.readLine());
			}
			for (int i=0;i<n-1; i++){
				for (int j=i;j<n; j++){
					if (hp[j]<hp[i]){
						String t_name=name[j];
						int t_hp=hp[j];
						name[j]=new String(name[i]);
						hp[j]=hp[i];
						name[i]=new String(t_name);
						hp[i]=t_hp;
					}
				}
			}
			for (int c=0;c<n;c++)
				System.out.println(name[c]);
			System.out.println("");
		
		} while (n>0);
		
		infile.close();
		}
}
