data Token = N Int | SO (Int -> Int -> Int) | PO (Int -> Int -> Int) | LP | RP
data Tree = Node Token Tree Tree [Guide] | Leaf Token [Guide]
data Branch = LeftBranch | RightBranch deriving (Eq)
type Guide = (Token, Branch, Tree)

instance Eq Token where
  (N _) == (N _) = True
  (SO _) == (SO _) = True
  (PO _) == (PO _) = True
  LP == LP = True
  RP == RP = True
  _ == _ = False

instance Ord Token where
  LP `compare` LP = EQ
  LP `compare` _ = GT
  _ `compare` LP = LT
  (N _) `compare` (N _) = EQ
  (N _) `compare` _ = GT
  _ `compare` (N _) = LT
  (SO _) `compare` (SO _) = EQ
  (SO _) `compare` _ = GT
  _ `compare` (SO _) = LT
  (PO _) `compare` (PO _) = EQ
  (PO _) `compare` _ = GT
  _ `compare` (PO _) = LT
  RP `compare` RP = EQ

instance Show Token where
  show (N n) = show n
  show (SO _) = "SO"
  show (PO _) = "PO"
  show LP = "----("
  show RP = ")----"

instance Show Tree where
  show (Leaf t g) = replicate (length g) '|' ++ (show t) ++ "\n"
  show n@(Node t l r g) = replicate (length g) '|' ++ ((show t) ++ ('\n':(show (moveDown n LeftBranch) ++ (show (moveDown n RightBranch)))))

setGuide :: Tree -> [Guide] -> Tree
setGuide (Node t l r _) g = Node t l r g
setGuide (Leaf t _) g = Leaf t g

moveDown :: Tree -> Branch -> Tree
moveDown (Node t l r g) LeftBranch = setGuide l ((t, LeftBranch, r):g)
moveDown (Node t l r g) RightBranch = setGuide r ((t, RightBranch, l):g)
moveDown _ _ = error "error: Try move down from Leaf -> moveDown"

moveUp :: Tree -> Tree
moveUp t@(Node _ _ _ ((u, branch, o):g))
  |branch == LeftBranch = Node u (setGuide t []) o g
  |branch == RightBranch = Node u o (setGuide t []) g
moveUp t@(Leaf _ ((u, branch, o):g))
  |branch == LeftBranch = Node u (setGuide t []) o g
  |branch == RightBranch = Node u o (setGuide t []) g
moveUp t@(Node _ _ _ []) = t
moveUp l = l

moveTop :: Tree -> Tree
moveTop t@(Node _ _ _ []) = t
moveTop l@(Leaf _ []) = l
moveTop t@(Node _ _ _ ((v, branch, o):gr)) = case branch of
  LeftBranch -> moveTop (Node v t o gr)
  RightBranch -> moveTop (Node v o t gr)
moveTop l@(Leaf _ ((v, branch, o):gr)) = case branch of
  LeftBranch -> moveTop (Node v l o gr)
  RightBranch -> moveTop (Node v o l gr)

moveUpUntil :: (Token -> Bool) -> Tree -> Tree
moveUpUntil _ n@(Node _ _ _ []) = n
moveUpUntil _ l@(Leaf _ []) = l
moveUpUntil f n@(Node _ _ _ ((t, _, _):_))
  |f t = moveUpUntil f (moveUp n)
  |otherwise = n
moveUpUntil f l@(Leaf _ ((t, _, _):_))
  |f t = moveUpUntil f (moveUp l)
  |otherwise = l

findUpFirstNode :: (Token -> Bool) -> Tree -> Tree
findUpFirstNode _ n@(Node _ _ _ []) = n
findUpFirstNode _ l@(Leaf _ []) =l
findUpFirstNode f n@(Node t _ _ _)
  |f t = n
  |otherwise = findUpFirstNode f (moveUp n)
findUpFirstNode f l@(Leaf t _)
  |f t = l
  |otherwise = findUpFirstNode f (moveUp l)

getOutPare :: Tree -> Tree
getOutPare = moveUp . findUpFirstNode (/= LP)

makeTree :: [Guide] -> [String] -> Tree
makeTree g [num] = moveTop $ Leaf (N (read num :: Int)) g
makeTree g ("(":sr) = makeTree ((LP, LeftBranch, (Leaf RP [])):g) sr
makeTree g [num, ")"]= moveTop $ Leaf (N (read num :: Int)) g
makeTree g (num:")":o:sr) = case o of
  "+" -> makeTree ((SO (+), RightBranch, top):(getGuide top)) sr
  "-" -> makeTree ((SO (-), RightBranch, top):(getGuide top)) sr
  "*" -> makeTree ((PO (*), RightBranch, ptop):(getGuide ptop)) sr
  "/" -> makeTree ((PO (div), RightBranch, ptop):(getGuide ptop)) sr
  where
    right = getOutPare (Leaf (N (read num :: Int)) g)
    top = moveUpUntil (/=LP) right
    ptop = moveUpUntil (<= (PO (*))) right
makeTree g (num:o:sr) = case o of
  "+" -> makeTree ((SO (+), RightBranch, top):(getGuide top)) sr
  "-" -> makeTree ((SO (-), RightBranch, top):(getGuide top)) sr
  "*" -> makeTree ((PO (*), RightBranch, ptop):(getGuide ptop)) sr
  "/" -> makeTree ((PO (div), RightBranch, ptop):(getGuide ptop)) sr
  where
    right = Leaf (N (read num :: Int)) g
    top = moveUpUntil (/=LP) right
    ptop = moveUpUntil (<= (PO (*))) right

getGuide :: Tree -> [Guide]
getGuide (Node _ _ _ g) = g
getGuide (Leaf _ g) = g

splitAtOperator :: String -> [String]
splitAtOperator "=" = [""]
splitAtOperator ")=" = ["", ")"]
splitAtOperator (s:sr)
  |s == '+' || s == '-' || s == '*' || s == '/' = []:[s]:(splitAtOperator sr)
  |s == '(' = "(":(splitAtOperator sr)
  | s == ')' = []:(')':(head (splitAtOperator sr))):(tail (splitAtOperator sr))
  |otherwise = (s:head (splitAtOperator sr)):(tail (splitAtOperator sr))

expressToTree :: String -> Tree
expressToTree = (makeTree []) . splitAtOperator

culcurateTree :: Tree -> Int
culcurateTree (Leaf t _) = case t of
  (N n) -> n
  _ -> error "error: Invalid Tree -> with unexpected Leaf"
culcurateTree n@(Node LP left _ _) = culcurateTree (moveDown n LeftBranch)
culcurateTree n@(Node (SO f) left right _) = f (culcurateTree (moveDown n LeftBranch)) (culcurateTree (moveDown n RightBranch))
culcurateTree n@(Node (PO f) left right _) = f (culcurateTree (moveDown n LeftBranch)) (culcurateTree (moveDown n RightBranch))
culcurateTree _ = error "error: Invalid Tree"

solve :: String -> Int
solve = culcurateTree . expressToTree

main :: IO ()
main = do
  n <- getLine
  expressions <- fmap lines getContents
  mapM_ (print . solve) expressions