data Token = LP | N Int | SO (Int -> Int -> Int) | PO (Int -> Int -> Int) | RP
data Branch = LeftBranch | RightBranch
type Guide = (Token, Branch, Tree)
data Tree = Node Token Tree Tree [Guide] | Leaf Token [Guide]
instance Eq Token where
  LP == LP = True
  (N _) == (N _) = True
  (SO _) == (SO _) = True
  (PO _) == (PO _) = True
  RP == RP = True
  _ == _ = False
instance Ord Token where
  LP `compare` LP = EQ
  LP `compare` _ = GT
  _ `compare` LP = LT
  (SO _) `compare` (SO _) = EQ
  (SO _) `compare` _ = GT
  _ `compare` (SO _) = LT
  (PO _) `compare` (PO _) = EQ
  (PO _) `compare` _ = GT
  _ `compare` (PO _) = LT
  (N _) `compare` (N _) = EQ
  (N _) `compare` _ = GT
  _ `compare` (N _) = LT
  RP `compare` RP = EQ
instance Show Token where
  show LP = "("
  show RP = ")"
  show (N n) = (show n)
  show (SO _) = "SO"
  show (PO _) = "PO"
instance Show Tree where
  show (Leaf t g) = replicate (length g) '|' ++ (show t ++ "\n")
  show n@(Node t _ _ g) = replicate (length g) '|' ++ (show t ++ ('\n':(show (moveLeft n) ++ show (moveRight n))))
moveUp :: Tree -> Tree
moveUp n@(Node _ _ _ ((token, branch, other):gr)) = case branch of
  LeftBranch -> Node token n other gr
  RightBranch -> Node token other n gr
moveUp l@(Leaf _ ((token, branch, other):gr)) = case branch of
  LeftBranch -> Node token l other gr
  RightBranch -> Node token other l gr
moveUp t = t
moveTop :: Tree -> Tree
moveTop = moveUpWhile (const True)
moveUpWhile :: (Token -> Bool) -> Tree -> Tree
moveUpWhile _ n@(Node _ _ _ []) = n
moveUpWhile _ l@(Leaf _ []) = l
moveUpWhile f n@(Node t _ _ _)
  |f t = moveUpWhile f (moveUp n)
  |otherwise = n
moveUpWhile f l@(Leaf t _)
  |f t = moveUpWhile f (moveUp l)
  |otherwise = l
findLast :: (Token -> Bool) -> Tree -> Tree
findLast f n@(Node _ _ _ ((t, _, _):_))
  |f t = findLast f (moveUp n)
  |otherwise = n
findLast f l@(Leaf _ ((t, _, _):_))
  |f t = findLast f (moveUp l)
  |otherwise = l
findLast _ n = n
moveDown :: Branch -> Tree -> Tree
moveDown LeftBranch (Node t (Node token left right _) r g) = Node token left right ((t, LeftBranch, r):g)
moveDown RightBranch (Node t l (Node token left right _) g) = Node token left right ((t, RightBranch, l):g)
moveDown LeftBranch (Node t (Leaf token _) r g) = Leaf token ((t, LeftBranch, r):g)
moveDown RightBranch (Node t l (Leaf token _) g) = Leaf token ((t, RightBranch, l):g)
moveDown _ _ = error "error: Can't move down"
moveLeft :: Tree -> Tree
moveLeft n@(Node {}) = moveDown LeftBranch n
moveLeft _ = error "error: Can't move Left"
moveRight :: Tree -> Tree
moveRight n@(Node {}) = moveDown RightBranch n
moveRight _ = error "error: Can't move Right"
moveUpForPO :: Tree -> Tree
moveUpForPO = findLast (==(PO (+)))
moveUpForSO :: Tree -> Tree
moveUpForSO = findLast (<=(SO (+)))
getoutPare :: Tree -> Tree
getoutPare = moveUpWhile (/=LP)
insertNode :: Tree -> Token -> Tree
insertNode o@(Node _ _ _ g) t = Leaf RP ((t, RightBranch, o):g)
insertNode l@(Leaf _ g) t = Leaf RP ((t, RightBranch, l):g)
guide :: Tree -> [Guide]
guide (Leaf _ g) = g
guide (Node _ _ _ g) = g
makeTree :: Tree -> [String] -> Tree
makeTree g ("(":sr) = makeTree (moveLeft top) sr
  where
    top = Node LP (Leaf RP []) (Leaf RP []) (guide g)
makeTree g (")":")":sr) = makeTree (getoutPare g) (")":sr)
makeTree g (num:")":")":sr) = makeTree gp (")":sr)
  where
    gp = getoutPare (Leaf (N (read num :: Int)) (guide g))
makeTree g (num:")":o:sr) = case o of
  "+" -> makeTree (insertNode so (SO (+))) sr
  "-" -> makeTree (insertNode so (SO (-))) sr
  "*" -> makeTree (insertNode po (PO (*))) sr
  "/" -> makeTree (insertNode po (PO (div))) sr
  otherwise -> error "error: line 89"
  where
    right = getoutPare (Leaf (N (read num :: Int)) (guide g))
    so = moveUpForSO right
    po = moveUpForPO right
makeTree g (")":o:sr) = case o of
  "+" -> makeTree (insertNode so (SO (+))) sr
  "-" -> makeTree (insertNode so (SO (-))) sr
  "*" -> makeTree (insertNode po (PO (*))) sr
  "/" -> makeTree (insertNode po (PO (div))) sr
  otherwise -> error "error: line 99"
  where
    right = getoutPare g
    so = moveUpForSO right
    po = moveUpForPO right
makeTree g [num, ")"] = moveTop (Leaf (N (read num :: Int)) (guide g))
makeTree g (num:o:sr) = case o of
  "+" -> makeTree (insertNode so (SO (+))) sr
  "-" -> makeTree (insertNode so (SO (-))) sr
  "*" -> makeTree (insertNode po (PO (*))) sr
  "/" -> makeTree (insertNode po (PO (div))) sr
  otherwise -> error "error: line 111"
  where
    right = Leaf (N (read num :: Int)) (guide g)
    so = moveUpForSO right
    po = moveUpForPO right
makeTree g [")"] = moveTop g
makeTree g [num] = moveTop (Leaf (N (read num :: Int)) (guide g))
makeTree _ _ = error "error: line 118"
splitAtOperator :: String -> [String]
splitAtOperator "=" = [[]]
splitAtOperator (o:sr)
  |o == '+' || o == '-' || o == '*' || o == '/' = []:[o]:rest
  |o == '(' = [o]:rest
  |o == ')' = []:(o:(head rest)):(tail rest)
  |otherwise = (o:(head rest)):(tail rest)
  where
    rest = splitAtOperator sr
expToTree :: String -> Tree
expToTree = (makeTree (Leaf RP [])) . splitAtOperator
culTree :: Tree -> Int
culTree (Leaf (N n) _) = n
culTree (Node (SO f) l r _) = f (culTree l) (culTree r)
culTree (Node (PO f) l r _) = f (culTree l) (culTree r)
culTree (Node LP l _ _) = culTree l
culTree _ = error "error: Can't culcurate expression Tree"
solve :: String -> Int
solve = culTree . expToTree
main :: IO ()
main = do
  _ <- getLine
  line <- fmap lines getContents
  mapM_ (print . solve) line