The presidential election of the Republic of Democratia runs in several stages.
You may assume the following about this election.
Every district of every stage therefore has exactly one winner, and no tie occurs.
Write a program that finds the smallest number of votes with which a candidate can win the election. Suppose the final-stage district consists of three first-stage districts whose numbers of eligible voters are 123, 4567 and 89. The minimum number of votes needed to win is 107, taking 62 votes in the first district and 45 in the third. Even if the other candidate took all 4567 votes of the second district, that candidate would still lose. The system may look unfair, but take it as given.
The whole input has this shape.
the number of datasets (= n)
1st dataset
2nd dataset
...
n-th dataset
The number of datasets n is at most 100.
The number of eligible voters of each district and the part-whole relation among districts are written in the following notation.
[c], where c is the number of eligible voters of that district.[d1d2...dm], where d1 through dm are its stage k−1 sub-districts written in the same notation.A first-stage district with 123 eligible voters is written as [123]. A second-stage district made of three first-stage districts with 123, 4567 and 89 eligible voters is written as [[123][4567][89]].
Each dataset is one line holding the string that denotes the final-stage district in this notation. The input satisfies the following.
0 through 9 and the square brackets [ and ], and its length is between 11 and 10000, inclusive.The number of stages is the same across the whole country, so a string such as [[[9][9][9]][9][9]] never appears in the input. A district of the second or a later stage always contains several districts of the previous stage, so [[[[9]]]] never appears either.
For each dataset, print in one line the minimum number of votes needed to win the presidential election. An output line must contain no characters other than the digits of that number.