Program HTML(input,output);

const
   maxwidth = 80;
   maxlines = 750;
   sep	    = Chr(9);    (* a tab, randomly chosen separator of columns :*)

var
   table		: array [1..MAXLINES] of string[maxwidth];
   width,totwidth	: array [1..MAXWIDTH] of integer;
   lines,columns,curcol	: integer;
   sym			: string[MAXWIDTH];
   r,runs   : integer;
   firsttime: boolean;
   c        : char;     (* Yeah, ugly as hell... *)

Procedure InitTable;
var
   i : integer;
begin
   lines:=0;
   columns:=0;
   for i:=1 to MAXLINES do table[i]:='';
   for i:=1 to MAXWIDTH do width[i]:=0;
end; { InitTable }


Procedure NextSym;
begin
   if firsttime then begin
	    Read(c);
      firsttime:=false;
   end;
   while (c<=' ') do Read(c);
   if (c='<') then begin
      sym:='<';
      Read(c);
      while (c<>'>') do begin
     SetStrLen(sym,StrLen(sym)+1);
     sym[StrLen(sym)]:=c;
	 			Read(c);
      end;
      StrAppend(sym,'>');
      Read(c);
   end else begin
     SetStrLen(sym,1);
     sym[1]:=c;
      Read(c);
      while (c>' ') and (c<>'<') do begin
     SetStrLen(sym,StrLen(sym)+1);
     sym[StrLen(sym)]:=c;
				 Read(c);
      end;
   end;
(*   WriteLn('`',sym,''''); *)
end; { NextSym }

Procedure ReadItem;
var
   itemwidth : integer;
begin
   if (sym='<TD>') then begin
   	  curcol:=curcol+1;
      if (curcol>columns) then columns:=columns+1;
      itemwidth:=0;
      if (curcol>1) then table[lines]:=table[lines]+SEP;
      NextSym; (* useful string or  *)
      while (sym<>'</TD>') do begin
		  	 if (itemwidth>0) then begin
	    		  itemwidth:=itemwidth+1;
	    			table[lines]:=table[lines]+' ';
				 end;
	 			 itemwidth:=itemwidth+StrLen(sym);
         table[lines]:=table[lines]+sym;
      	 if (itemwidth>width[curcol]) then width[curcol]:=itemwidth;
      	 NextSym;
      end;
   end;
end; { ReadItem }

Procedure ReadLine;
var
   i,strlentab : integer;
begin
   if (sym='<TR>') then begin
      lines:=lines+1;
      curcol:=0;
      NextSym; (*  or  *)
      while (sym<>'</TR>') do begin
	 ReadItem;
	 NextSym;
      end;
     strlentab:=StrLen(table[lines]);
     SetStrLen(table[lines],MAXWIDTH);
      for i:=strlentab+1 to MAXWIDTH do table[lines][i]:=SEP;
   end;
end; { ReadLine }

Procedure ReadTable;
begin
   NextSym; (* assume this IS  *)
   NextSym;(*  or 
*) while (sym<>'</TABLE>') do begin ReadLine; NextSym; end; end; { ReadTable } Procedure WriteRuler; var i,j : integer; begin for i:=1 to columns do begin Write('+'); for j:=0 to width[i] do Write('-'); if (width[i]>0) then Write('-'); end; WriteLn('+'); end; { WriteRuler } Procedure WriteTable; var i,j,k : integer; (* run through table&width *) pos : integer; (* run over screen (without delimiters) *) begin totwidth[1]:=width[1]; for k:=2 to MAXWIDTH do totwidth[k]:=totwidth[k-1]+width[k]; WriteRuler; for i:=1 to lines do begin k:=1; pos:=1; for j:=1 to columns do begin Write('| '); while (table[i][k]<>SEP) do begin Write(table[i][k]); k:=k+1; (* printed char *) pos:=pos+1; end; k:=k+1; (* SEP itself *) while (pos<=totwidth[j]) do begin Write(' '); pos:=pos+1; end; if (width[j]>0) then Write(' '); end; WriteLn('|'); WriteRuler; end; end; { WriteTable } begin firsttime:=true; ReadLn(runs); for r:=1 to runs do begin InitTable; ReadTable; WriteTable; end; end.