LearnToCP
Sign in

Navigation

HomeRoadmapProblemsAbout Us

Theory

Contest Knowledge
Selecting an IDE
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion Find
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPTree DPBitmask DPDigit DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Postfix Expression

EasyProblem #26
Time LimitMemoryInputOutputTAGS
1 s64 MBstdinstdout
#stack#strings

The values fit in a long long, but not in an int.

Postfix notation (also called reverse Polish notation, in honor of the Polish logician Jan Lukasiewicz who invented it) writes an operator after its two operands instead of between them. So instead of 3 + 5 we write 3 5 +. The big advantage: postfix expressions need no brackets - the order of operations is never ambiguous.

Your task is to compute the value of postfix expressions built from one-digit numbers and the operators + and *, written with no spaces.

Input

First line of input will be a single integer t - the number of testcases.
Each of the next t lines contains one valid postfix expression.

Output

For every expression print a single line with its value.

Example

Input
2
12+3*
11+2*345+*+
Output
9
31

The first expression is (1+2)*3. The second is (1+1)*2+3*(4+5).

Constraints

1≤t≤1000
Each expression has at most 199 characters.
The value of the expression, and of every intermediate result, is at most 1018.


This problem was adapted, with permission, from Vrednost postfiksnog izraza, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in