program dwitetwo;
var
  team1: array[0..100] of integer;
  team2: array[1..100] of string;

  aa, current: integer;
  where: integer;

  popans: string;

  useless: char;

  s: integer;
  n: string;

  x: integer;

procedure change1(var a: integer; var b: integer);
  var
    temp: integer;
  begin
    temp := b;
    b := a;
    a := temp;
  end;

procedure change2(var a: string; var b: string);
  var
    temp: string;
  begin
    temp := b;
    b := a;
    a := temp;
  end;

procedure push(a: integer; b: string);
  var
    where: integer;
  begin
    where := current;

    team1[where] := a;
    team2[where] := b;

    inc(current);

    while team1[where] > team1[where div 2] do
      begin
        change1(team1[where], team1[where div 2]);
        change2(team2[where], team2[where div 2]);

        where := where div 2;
      end;
  end;

procedure pop;
  begin
    popans := team2[1];

    dec(current);

    team1[1] := team1[current];
    team2[1] := team2[current];

    team1[current] := 0;
    team2[current] := '';

    where := 1;

    while (team1[where] < team1[where * 2]) or (team1[where] < team1[where*2+1]) do
      if team1[where*2] > team1[where * 2+1] then
        begin
          change1(team1[where], team1[where*2]);
          change2(team2[where], team2[where*2]);
          where := where * 2;
        end
      else
        begin
          change1(team1[where], team1[where*2+1]);
          change2(team2[where], team2[where*2+1]);
          where := where * 2 + 1;
        end;
  end;

begin
  assign(input, 'DATA2.txt');
  reset(input);
  assign(output, 'OUT2.txt');
  rewrite(output);


  for aa := 1 to 5 do
  begin
  fillchar(team1, sizeof(team1), 0);

  team1[0] := maxint;
  current := 1;

  for x := 1 to 5 do
    begin
      readln(s, useless, n);

      push(s, n);
    end;

  for x := 1 to 5 do
    begin
      pop;

      writeln(popans);
    end;
  end;
  readln;
end.
