SIMPLFYIT - Simplify IT
Nowadays computer can do a lot of things, can't it? But how? Yes, you are the answer. The biggest secret behind capability of the computer is some programmer like you. They wrote some programs and computer only follow these. That's it.
Anyway, your task is to write some program that will be followed by the computer to simplify an algebraic expression.
The initial expression is 0. There will be 3 types of command. These are:
For command-type 1, you have to add <expression> to the existing expression. For command-type 2, you have to subtract <expression> from the existing expression. For command-type 3, you have to print the existing expression.
For simplicity the <expression> will contain 2 variables, such as 'a' and 'b'. and the <expression> will be in the following format:
where 'C' is a constant, 'a' and 'b' are variable and 'P' and 'Q' are the power of 'a' and 'b' respectively.
The first line will contain an integer T which denotes the number of test cases. For T cases, there will be a number N which denotes the number of command for this case. Then the following N lines contain commands for the case mentioned above.
The absolute value of 'C', 'P' and 'Q' is less than or equal to 10. T and N will be a positive integer less than or equal to 101.
For each command type 3, print the current expression. Print the part first which has the lower power of 'a'. If the power of 'a' is equal then print the part first which has the lower power of 'b'.
Input: 1 11 3 1 2a1b1 3 1 1a2b0 3 2 1a0b2 3 2 1a5b1 3 1 1a2b0 3 Output: 0 2ab 2ab+a2 -b2+2ab+a2 -b2+2ab+a2-a5b -b2+2ab+2a2-a5b Note: In "-b2+2ab+2a2-a5b", 1st part has 'a' with power 0, 2nd part has 'a' with power 1, 3rd part has 'a' with power 2, 4th part has 'a' with power 5.