{$x+} program Arith; const LEN = 1010; type TNum = array [1..LEN] of Byte; TCharNum = array [1..LEN+1] of Char; procedure Sum(const A, B: TNum; var C: TNum); var CC, i: Integer; begin CC := 0; for i := LEN downto 1 do begin CC := A[i] + B[i] + CC; C[i] := CC mod 10; CC := CC div 10; end; end; procedure SumMulShlift(const B: TNum; var C: TNum; D, E: Integer); { C += B * D * 10^E } var CC, i: Integer; begin CC := 0; for i := LEN downto 1 do begin if i + E <= LEN then CC := C[i] + B[i + E] * D + CC else CC := C[i] + CC; C[i] := CC mod 10; CC := CC div 10; end; end; procedure Mul(const A, B: TNum; var C: TNum); var i: Integer; begin FillChar(C, SizeOf(C), 0); for i := LEN downto 1 do begin if B[i] > 0 then begin SumMulShlift(A, C, B[i], LEN - i); end; end; end; procedure Diff(const A, B: TNum; var C: TNum); var CC: Integer; i: Integer; begin CC := 0; for i := LEN downto 1 do begin CC := 10 + A[i] - B[i] - CC; C[i] := CC mod 10; CC := 1 - CC div 10; end; end; procedure Fix(var A: TNum; C: Integer); var i: Integer; begin for i := LEN downto LEN + 1 - C do A[i] := A[C + i - LEN]; for i := LEN - C downto 1 do A[i] := 0; end; function GetFirstPos(const A: TNum): Integer; var i: Integer; begin for i := 1 to LEN do begin if A[i] > 0 then begin GetFirstPos := i; Exit; end; end; GetFirstPos := LEN; end; procedure Print(Ch: Char; const A: Tnum; From: Integer); var B: TCharNum; OrigFrom: Integer; begin OrigFrom := From; while (From < Len) and (A[From] = 0) do begin if A[From + 1] = 0 then B[From] := ' ' else B[From] := Ch; Inc(From); end; if B[Len - 1] = ' ' then B[Len - 1] := Ch; while (From <= Len) do begin B[From] := Chr(A[From] + Ord('0')); Inc(From); end; B[From] := #0; WriteLn(@B[OrigFrom]); end; var N : Integer; C1, C2: Integer; Op: Char; C: Char; S1, S2, S3, SS: TNum; D, D1, D2, D3, D4, D5, E: Integer; begin ReadLn(N); while (N > 0) do begin Dec(N); C1 := 0; C2 := 0; Op := ' '; repeat Read(C); if (C >= '0') and (C <= '9') then begin Inc(C1); S1[C1] := Ord(C) - Ord('0'); end else if (C = '+') or (C = '-') or (C = '*') then begin Op := C; Break; end; until False; repeat if Eoln then Break; Read(C); if (C >= '0') and (C <= '9') then begin Inc(C2); S2[C2] := Ord(C) - Ord('0'); end else begin Break; end; until False; Fix(S1, C1); Fix(S2, C2); case Op of '+': begin Sum(S1, S2, S3); D1 := GetFirstPos(S1); D2 := GetFirstPos(S2) - 1; D3 := GetFirstPos(S3); D4 := LEN + 1; if D1 < D4 then D4 := D1; if D2 < D4 then D4 := D2; if D3 < D4 then D4 := D3; Print(' ', S1, D4); Print('+', S2, D4); for D := D4 to LEN do Write('-'); WriteLn; Print(' ', S3, D4); WriteLn; end; '-': begin Diff(S1, S2, S3); D1 := GetFirstPos(S1); D2 := GetFirstPos(S2) - 1; D3 := GetFirstPos(S3); D4 := LEN + 1; if D1 < D4 then D4 := D1; if D2 < D4 then D4 := D2; if D3 < D4 then D4 := D3; Print(' ', S1, D4); Print('-', S2, D4); for D := D4 to LEN do Write('-'); WriteLn; Print(' ', S3, D4); WriteLn; end; '*': begin Mul(S1, S2, S3); D1 := GetFirstPos(S1); D2 := GetFirstPos(S2) - 1; D3 := GetFirstPos(S3); D4 := LEN + 1; if D1 < D4 then D4 := D1; if D2 < D4 then D4 := D2; D5 := D4; if D3 < D5 then D5 := D3; for D := D5 to D4 - 1 do Write(' '); Print(' ', S1, D4); for D := D5 to D4 - 1 do Write(' '); Print('*', S2, D4); for D := D5 to D4 - 1 do Write(' '); for D := D4 to LEN do Write('-'); WriteLn; if D2 + 1 <> LEN then begin for E := LEN downto D2 + 1 do begin FillChar(SS, SizeOf(SS), 0); if S2[E] > 0 then SumMulShlift(S1, SS, S2[E], 0); Print(' ', SS, D5 + LEN - E); end; for D := D5 to LEN do Write('-'); WriteLn; end; Print(' ', S3, D5); WriteLn; end; end; end; end.