


You are given ‘EXP’ = {‘2’,’3’,’1’,’*’,’+’,’9’,’-’}. Then the result will be -4.
2 3 1 * + 9 -
- : ( ) - ( )
9 : ( ) - (9)
+ : ( ( ) + ( ) ) - (9)
'*': ( ( ) + ( ( ) * ( ) ) ) - (9)
1 : ( ( ) + ( ( ) * (1) ) ) - (9)
3 : ( ( ) + ( (3) * (1) ) ) - (9)
2 : ( (2) + ( (3) * (1) ) ) - (9)
Result = (2 + 3) - 9 = 5 - 9 = -4
The first line of the input contains a single integer 'T', representing the number of test cases.
The first line of each test case contains an integer ‘N’, representing the size array ‘EXP’.
The second line of each test case contains ‘N’ space-separated tokens(operators and operands) of the given expression.
For each test case, print an integer obtained by evaluating the given expression.
Print the output of each test case in a separate line.
Operators will only include the basic arithmetic operators like '*', '/', '+', and '-'.
The operand can contain multiple digits.
The operators and operands will have space as a separator between them.
There won’t be any brackets in the given expression.
The answer could be very large, output your answer modulo (10^9+7). Also, use modular division when required.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5000
1 <= NUM <= 100
Where ‘N’ denotes the length of the given expression, and ‘NUM’ denotes the operand.
Time Limit: 1 sec
The idea is to use a stack to store the operands. Whenever an operator is encountered, we pop the top two numbers from the stack, perform the operation and push the result back to the stack. Finally, when the traversal is completed, the number left in the stack is the final answer.
Algorithm :