const
  Len   = 20;
  High  = 10;
  Inp1  = 'input1.txt';
  Inp2  = 'input2.txt';
  Out1  = 'output1.txt';
type
 Pty = ^ty;
 ty = record
   name : Char;
   next : Pty;
 end;

var
  Root1,Root2,Nroot       : Pty;
  f1,f2                   : text;
{--------------------------------} {Finde Spase For New Iteme and includ it}
procedure Sort(var NRoot,Tec : pty;posl : Pty);
var
Pred,sled : Pty;
begin
  if Tec^.name < Nroot^.name Then begin
     Tec^.next := Nroot;
     Nroot := Tec;
  end
  else begin
     Pred :=  Nroot;
     Sled := Nroot^.next;
     while (Tec^.name >= Sled^.name) and (Sled <> Posl) do begin
        Pred := Sled;
        Sled := Sled^.next;
     end;
     pred^.next := Tec;
     Tec^.next  := Sled;
  end;
end;
{--------------------------------}

{read m from file==========================================}{P1}
Procedure ReadM(var root : pty;inp : string);
var
Tec,Pred : Pty;

begin
  Assign(f1,Inp);
  Reset(f1);
  New(tec);
  Read(f1,Tec^.Name);
  Root := Tec;
  Root^.next := nil;
  Pred := Tec;
  While not(Eof(F1)) do begin
    New(Tec);
    Read(f1,Tec^.Name);
    Sort(Root,Tec,nil);
  end;
  close(f1);
end;
{==========================================}
{==========================================}    {p2}
Procedure OuttoFils(Root : pty);
begin
  Assign(F2,Out1);
  ReWrite(f2);

  {write to files}
  while root<>nil do Begin
    Write(f2,Root^.name);
    root := root^.next;
  end;

  close(f2);
end;
{==========================================}
Procedure P3(Var Root1,Root2,NewRoot : pty);      {p3}
var
  Tec,Sled : Pty;
begin
  NewRoot := Root1;
  Tec := Root1^.next;
  While Tec^.next <> nil do begin
    Tec := Tec^.next;
  end;
  Tec^.next := Root2;
  Tec  := Root1;
  Sled := Root1^.next;

  While Sled <> nil do begin
    If Tec^.name > Sled^.name Then begin
         Tec^.next := Sled^.next;
         Sort(Root1,Sled,Sled^.next);
    end;
    Tec := Sled;
    Sled :=Sled^.next;
  end;

end;
{==========================================}
begin
  ReadM(Root1,inp1);
  ReadM(Root2,inp2);
  P3(Root1,Root2,NRoot);
  OuttoFils(NRoot);
end.