const
 count		= 20;
 name_len       = 11;
 phone_len      = 9;
 separators     = [4,7];

type TPhoneEntry = record
  Name  : string[name_len];
  Phone : string[phone_len];
end;

var
  DB	     : array [1..count] of TPhoneEntry;
  INP	     : file of TPhoneEntry;
  k,i        : integer;

Procedure Exchange(var s1,s2 : TPhoneEntry);
var
 tmp : TPhoneEntry;
begin
  tmp := s1;
  s1 := s2;
  s2 := tmp;
end;

Procedure SortNum(num : integer);
var
 i, j : integer;
begin
  for i := 1 to count-1 do
   for j:=count-1 downto i do
    begin
     if DB[j+1].Phone[num] > DB[j].Phone[num] then Exchange(DB[j], DB[j+1]);
    end;
end;

begin
  Assign(INP,'phones.dat');
  {$I-}
    Reset(INP);
    If IOResult <> 0 then
     begin
       WriteLn('File ''phones.dat'' not found');
       Halt(255);
     end;
  {$I+}

  k:=1;
  while not eof(inp) do
   begin
     Read(INP,DB[k]);
     inc(k);
   end;

  Close(INP);

  for k := phone_len downto 1 do
   if not (k in Separators) then
     SortNum(k);

  for k := 1 to count do
   begin
    Write(DB[k].Name);
    for i:=1 to 11-Length(DB[k].Name) do Write(' ');
    WriteLn(DB[k].Phone);
   end;
  WriteLn('End.');
end.
