{starting number:16}
{vyreseno 1,2,3-castecne}

const Maxc=100;
      Flag:boolean=false;
      Align:byte=0;
      Span:boolean=false;
var Inf,Outf:text;
    Outfs,Infs,s:string;
    I,N,J,K:integer;
    Line,Cols,LineLength:Integer;
    Needs:array[0..Maxc]of integer;
    Left,Spans:array[1..Maxc,1..Maxc]of boolean;
    Center:array[1..Maxc,1..Maxc]of byte;

procedure LeftBlanks(s:string;var uk:integer);{vynecha mezery}
begin
 while S[uk]=' ' do inc(uk);
end;

function UpperStr(s:string):string;{case insensitive}
var poms:string;
    i:integer;
begin
 poms:='';
 for i:=1 to Length(s) do poms:=poms+Upcase(s[i]);
 UpperStr:=poms;
end;

function CheckWord(s:string):string;{prevede slova zacinajici na &}
var i:integer;
    spom,sp,s1:string;
begin
 s1:=s;i:=pos('&',s1);spom:='';
 while i>0 do begin
  spom:=spom+Copy(s1,1,i-1);
  s1:=Copy(s1,i,length(s1)-i+1);
  sp:=UpperStr(copy(s1,1,3));
  if sp='&LT' then begin spom:=spom+'<';s1:=Copy(s1,4,length(s1)-3);end else
     if sp='&GT' then begin spom:=spom+'>';s1:=Copy(s1,4,length(s1)-3);end
     else begin spom:=spom+'&';s1:=Copy(s1,5,length(s1)-4);end;
  i:=pos('&',s1);spom:='';
 end;
 if i=0 then spom:=spom+s1;
 checkWord:=spom;
end;

function Max(a,b:integer):integer;  {vraci maximum}
begin if A>b then Max:=a else Max:=b;end;

function Min(a,b:integer):integer;  {vraci minimum}
begin if A<b then Min:=a else Min:=b;end;

procedure Abort(s:string);    {chybova hlaseni}
begin
  writeln(s);halt;
end;

function GetWord(s:string;var uk:integer):string; {poskytne slovo z radky}
var i,c:integer;
    spom:string;
begin
  LeftBlanks(s,uk);
  i:=uk;
  if (s[uk]='<')then begin while (s[uk]<>'>')and(uk<length(s)) do inc(uk);inc(uk);end else
  while (uk<length(s))and(s[uk]<>' ')and(s[uk]<>'<') do inc(uk);
  c:=uk;
  if flag then flag:=false;
  if (uk=length(s))and(s[uk-1]='>')then begin
    if flag then flag:=false else begin dec(c);flag:=true;end;
  end else
  if uk<>length(s) then dec(c);
  spom:='';
  spom:=Copy(s,i,c-i+1);
  CheckWord(spom);
  GetWord:=spom;
end;

function CharsNeeded(s:string;var uk:integer):integer;    {spocita delku radku}
var i,n,j,x,y,c:integer;
    s1,s2,s3:string;
begin
  n:=0;
  s1:=getword(s,uk);
  while (uk<length(s))or(flag) do begin
    s1:=getword(s,uk);
    if s1[1]='<' then
    begin
     s1:=upperstr(s1);
      if (pos('TH',s1)>0)or(pos('TD',s1)>0) then begin
        if pos('COLSPAN',s1)>0 then begin
           s3:=copy(s1,pos('COLSPAN',s1),length(s1)-pos('COLSPAN',s1)+1);
           j:=pos('=',s3)+1;c:=pos(' ',s3);if c=0 then c:=Maxint;c:=Min(c,pos('>',s3));
           x:=c-j;s3:=copy(s3,j,x);Val(s3,x,j);
           for j:=n+2 to n+x do spans[Line,j]:=true;Center[line,n+1]:=x;
        end;
        if n<>0 then begin s2:=s2+' ';Needs[n]:=Max(length(s2),Needs[n]);end;
        inc(n);s2:='|';
        while Spans[Line,n] do inc(n);
      end
    end else s2:=s2+' '+s1;
  end;
  if n<>0 then begin s2:=s2+' ';Needs[n]:=Max(length(s2),Needs[n]);end;j:=0;
  for i:=1 to n do j:=j+Needs[i];Cols:=max(cols,n);
  CharsNeeded:=j+1;
end;


procedure FlushLine(s:string;var uk:integer);
var i,n,j,f,i1,i2:integer;
    s1,s2:string;

procedure WriteWord(s:string;var n:integer);
var i1,i2,x,c,v:integer;
begin
if spans[line,n] then begin
      Write(outf,'|');
      for c:=2 to needs[n] do write(outf,' ');
 end else
if center[line,n]>1 then begin
v:=0;x:=center[line,n];for c:=n to n+x-1 do v:=v+Needs[c];
if align=1 then begin
      Write(outf,'|');
      i1:=v-1;i2:=(i1-length(s))div 2;if (i1-length(s))mod 2=1 then inc(i2);
      for i1:=1 to i2 do write(outf,' ');i1:=i2+1;
      write(outf,s);i1:=i1+length(s);
      for i2:=i1+1 to v do write(outf,' ');
end else
if align=0 then begin
      Write(outf,'|');
      write(outf,' ');i1:=2;
      write(outf,s);i1:=i1+length(s);
      for i2:=i1+1 to v do write(outf,' ');
end else
if align=2 then begin
      Write(outf,'|');
      i1:=v-length(s)-2;
      for i2:=1 to i1 do write(outf,' ');
      write(outf,s);
      write(outf,' ');
end;
n:=n+x-1;
end else
if align=1 then begin
      Write(outf,'|');
      i1:=Needs[n]-1;i2:=(i1-length(s))div 2;if (i1-length(s))mod 2=1 then inc(i2);
      for i1:=1 to i2 do write(outf,' ');i1:=i2+1;
      write(outf,s);i1:=i1+length(s);
      for i2:=i1+1 to Needs[n] do write(outf,' ');
end else
if align=0 then begin
      Write(outf,'|');
      write(outf,' ');i1:=2;
      write(outf,s);i1:=i1+length(s);
      for i2:=i1+1 to Needs[n] do write(outf,' ');
end else
if align=2 then begin
      Write(outf,'|');
      i1:=Needs[n]-length(s)-2;
      for i2:=1 to i1 do write(outf,' ');
      write(outf,s);
      write(outf,' ');
end;
end;


begin
  s1:=getword(s,uk);
  n:=0;
  while (uk<length(s))or flag do begin
    s1:=getword(s,uk);
    if s1[1]='<' then begin
       if n<>0 then begin
          WriteWord(s2,n);
       end;
       s1:=upperstr(s1);
       if pos('TH',s1)>0 then align:=1 else
         if pos('TD',s1)>0 then align:=0;
       if pos('ALIGN=RIGHT',s1)>0 then align:=2 else
         if pos('ALIGN=LEFT',s1)>0 then align:=0 else
           if pos('ALIGN=CENTER',s1)>0 then align:=1;
       inc(n);s2:='';
    end else if s2='' then s2:=s1 else s2:=s2+' '+s1;
  end;
      if n<>0 then begin
        WriteWord(s2,n);
      end;
end;

procedure zpracuj;
var i,n,j,uk:integer;
begin

   for uk:=0 to maxc do Needs[uk]:=0;Cols:=0;
   Line:=0;
   while not Eof(inf) do begin
      readln(inf,s);uk:=1;inc(Line);
      Needs[0]:=Max(Needs[0],CharsNeeded(s,uk));
   end;
   if Needs[0]>LineLength then Abort('Error: Table is too long to fit line.');
   Close(inf);Reset(inf);
   write(outf,'+');for i:=2 to Needs[0]-1 do Write(outf,'-');Writeln(outf,'+');
   Line:=0;
   while not eof(inf) do begin
      readln(inf,s);uk:=1;inc(line);
      FlushLine(s,uk);
      writeln(outf,'|');
      if not eof(inf) then begin write(outf,'|');
         for i:=1 to Cols do begin
          if i<>1 then Write(outf,'+');
          for j:=2 to Needs[i] do Write(outf,'-');
         end;
         writeln(outf,'|');
      end;
   end;
   write(outf,'+');for i:=2 to Needs[0]-1 do Write(outf,'-');Writeln(outf,'+');
end;

begin
 if paramcount<2 then Abort('Not enough parameters...');n:=0;
 for i:=1 to paramcount do
   if pos('-',paramstr(i))=1 then begin n:=i;break;end;
 if n<>0 then begin
   s:=copy(paramstr(n),2,length(paramstr(n)));
   val(s,LineLength,k);if k<>0 then Abort('Length of line typed incorectly...');
 end else LineLength:=80;
 if n=1 then begin infs:=paramstr(2);outfs:=paramstr(3);end;
 if n=2 then begin infs:=paramstr(1);outfs:=paramstr(3);end;
 if (n=0)or(n>2) then begin infs:=paramstr(1);outfs:=paramstr(2);end;
 assign(inf,infs);
 {$I-}reset(inf);{$i+}
 if Ioresult<>0 then Abort('File '+infs+' not found...');
 assign(outf,outfs);rewrite(outf);
 for i:=1 to MaxC do
   for n:=1 to MaxC do begin Left[i,n]:=false;spans[i,n]:=false;center[i,n]:=1;end;
 zpracuj;
 Close(inf);Close(Outf);
end.