import java.util.*

fun main(args:Array<String>?): Unit {
    repeat(readLine()!!.trim().toInt()){
        println(Section.parse(readLine()!!.trim()).needToWin)
    }
}

sealed class Section {
    abstract val needToWin: Int
    class LowestSection(population: Int): Section(){
        override val needToWin: Int = population / 2 + 1
    }
    class HigherSection(lower: Collection<Section>): Section() {
        override val needToWin: Int by lazy{lower.map(Section::needToWin).sorted().take(lower.size / 2 + 1).sum()}
    }
    companion object {
        fun parse(expression: String): Section {
            tailrec fun sub(position: Int, stack: Stack<Stack<Section>>): Section {
                return if (position < expression.length) {
                    when(expression[position]) {
                        '[' -> {
                            stack.push(Stack())
                            sub(position + 1, stack)
                        }
                        ']' -> {
                            val top = stack.pop()
                            stack.peek().push(HigherSection(top))
                            sub(position + 1, stack)
                        }
                        else -> {
                            val end = (position until expression.length).find { expression[it] == ']' }!!
                            stack.peek().push(LowestSection((position until end).map { expression[it] - '0' }.reduce { acc, i -> acc * 10 + i }))
                            sub(end, stack)
                        }
                    }
                }else stack.peek().peek()
            }
            val stack = Stack<Stack<Section>>()
            stack.add(Stack())
            return sub(0, stack)
        }
    }
}
