def solve(s)
    if s[0] =~ /[0-9]/
        return (s.to_i / 2) + 1
    else
        level = 0
        i = 0
        while s[i] == "["
            level += 1
            i += 1
        end
        st = "]" * level
        sub = Array.new
        while true
            t = s.index(st)
            break if t == nil
            sub.push(solve(s[1...(t + level - 1)]))
            s = s[(t + level)...s.length]
        end
        sub.sort!
        ret = 0
        0.upto(sub.size / 2) do |i|
            ret += sub[i]
        end
        return ret
    end
end
n = gets.to_i
n.times do
    s = gets.chomp
    p solve(s[1...(s.length - 1)])
end