class Student implements Comparable{
String name;
int gpa;
@Override
public int compareTo(Object arg0) {
// TODO Auto-generated method stub
Student s = (Student)arg0;
if(gpa == s.gpa) return name.compareTo(s.name);
else if(gpa < s.gpa) return -1;
else if(gpa > s.gpa) return 1;
else return 0;
}
}
class Test {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Student[] s = new Student[n];
for(int i = 0;i < n;i ++){
s[i] = new Student();
s[i].name = in.next();
s[i].gpa = in.nextInt();
}
Arrays.sort(s);
for (Student ss : s)
{
System.out.println(ss.name);
System.out.println(ss.gpa);
}
}
}