Program letter;
Var
s:array [0..100] of string;
i,j,n:longint;
Begin
assign(input,'letter.in');
assign(output,'letter.out');
reset(input);
rewrite(output);
readln(n);
for i:=1 to n do readln(s[i]);
for i:=1 to n-1 do begin
for j:=i+1 to n do begin
if s[j,1]=s[i,1] then begin
if length(s[i])>length(s[j]) then begin
s[0]:=s[i];
s[i]:=s[j];
s[j]:=s[0];
end;
end
else begin
if ord(s[i,1])>ord(s[j,1]) then begin
s[0]:=s[i];
s[i]:=s[j];
s[j]:=s[0];
end;
end;
end;
end;
for i:=1 to n do writeln(s[i]);
close(input);
close(output);
End.