Let *X* be a set of all rational numbers and all numbers of form *q*√*r*, where *q* is a non-zero rational number and *r*
is an integer greater than 1. Here *r* must not have a quadratic number except for 1 as its divisor. Also, let *X*^{*} be a
set of all numbers which can be expressed as a sum of one or more elements in *X*.

A machine *Y* is a stack-based calculator which operates on the values in *X*^{*} and has the instructions shown in the table below.

push |
Pushes an integer specified in the operand onto the stack. |

add |
Pops two values |

sub |
Pops two values |

mul |
Pops two values |

div |
Pops two values |

sqrt |
Pops one value |

disp |
Pops one value |

stop |
Terminates calculation. The stack must be empty when this instruction is called. |

Table 1: Instruction Set for the Machine Y

A sufficient number of values must exist in the stack on execution of every instruction. In addition, due to the limitation of the machine Y, no more values can be pushed when the stack already stores as many as 256 values. Also, there exist several restrictions on values to be pushed onto the stack:

- For rational numbers, neither numerator nor denominator in the irreducible form may exceed 32,768 in its absolute value.
- For any element in
*X*of the form*q*√*r*= (*a*/*b*)√*r*, |*a*√*r*| ≤ 32,768 and |*b*| ≤ 32,768. - For any element in
*X*^{*}, each term in the sum must satisfy the above conditions.

The rules for the string representations of the values (on the machine Y) are as follows:

- A rational number is represented as either an integer or an irreducible fraction with a denominator greater
than 1. A fraction is represented as "<
*numerator*>/<*denominator*>". A sign symbol - precedes in case of a negative number. - A number of the form
*q*√*r*is represented as "<*string representation of q*>^{*}sqrt(*r*)" except for the case with*q*= ±1, in which the number is represented as "sqrt(*r*)" (*q*= 1) or "-sqrt(*r*)" (*q*= -1). - For the sum of two or more elements of
*X*, string representations of all the (non-zero) elements are con- nected using the binary operator +. In this case, all terms with the same rooted number are merged into a single term, and the terms must be shown in the ascending order of its root component. For the purpose of this rule, all rational numbers are regarded to accompany √1. - There is exactly one space character before and after each of the binary operator +. No space character appears at any other place.

The followings are a few examples of valid string representations:

0 1 -1/10 2*sqrt(2) + 1/2*sqrt(3) + -1/2*sqrt(5) 1/2 + sqrt(10) + -sqrt(30)

Your task is to write a program that simulates the machine Y.

The input is a sequence of instructions. Each line contains a single instruction. You may assume that every instruction is called in a legal way. The instruction stop appears only once, at the end of the entire input.

Output the strings which the machine Y will display. Write each string in a line.

push 1 push 2 sqrt div push 1 push 3 sqrt div add disp push 8 sqrt push 3 push 2 sqrt mul add disp stop

1/2*sqrt(2) + 1/3*sqrt(3) 5*sqrt(2)

Source: ACM-ICPC Japan Alumni Group Practice Contest
, for World Finals, Tokyo, Japan, 2007

http://acm-icpc.aitea.net/

http://acm-icpc.aitea.net/