import Data.Char
import Data.List

main = interact $ unlines . map (show . hierarchical_democracy) . tail . lines

hierarchical_democracy = head . extract . until decisive minimal . majority
 where decisive (_:x:_) = isDigit x

extract = map read . words . map (\x -> if isDigit x then x else ' ')

minimal xxs@(b:x1:x2:xs)
 | isDigit x1 = xxs
 | isDigit x2 = let (d,xs') = takeDistrict [] xxs in vote (extract d) ++ minimal xs' 
 | otherwise = b : minimal (tail xxs)
 where
  vote xs = "[" ++ show (sum (take (div (length xs + 1) 2) (sort xs))) ++ "]"
  takeDistrict acc (']':']':xs) = (reverse (']':']':acc), xs)
  takeDistrict acc (x:xs) = takeDistrict (x:acc) xs
minimal xs = xs

majority = concat . map half . groupBy digits
 where
  digits c1 c2 = isDigit c1 && isDigit c2
  half xs = if isDigit (head xs) then show (div (read xs + 1) 2) else xs