The presidential election in Republic of Democratia is carried out through multiple stages as follows.
You can assume the following about the presidential election of this country.
This means that each district of every stage has its winner (there is no tie).
Your mission is to write a program that finds a way to win the presidential election with the minimum number of votes. Suppose, for instance, that the district of the final stage has three sub-districts of the first stage and that the numbers of the eligible voters of the sub-districts are 123, 4567, and 89, respectively. The minimum number of votes required to be the winner is 107, that is, 62 from the first district and 45 from the third. In this case, even if the other candidate were given all the 4567 votes in the second district, s/he would inevitably be the loser. Although you might consider this election system unfair, you should accept it as a reality.
The entire input looks like:
the number of datasets (=n)
1st dataset
2nd dataset
…
n-th dataset
The number of datasets, n, is no more than 100.
The number of the eligible voters of each district and the part-whole relations among districts are denoted as follows.
For instance, an electoral district of the first stage that has 123 eligible voters is denoted as [123]. A district of the second stage consisting of three sub-districts of the first stage that have 123, 4567, and 89 eligible voters, respectively, is denoted as [[123][4567][89]].
Each dataset is a line that contains the character string denoting the district of the final stage in the aforementioned notation. You can assume the following.
The number of stages is a nation-wide constant. So, for instance, [[[9][9][9]][9][9]] never appears in the input. [[[[9]]]] may not appear either since each district of the second or later stage must have multiple sub-districts of the previous stage.
For each dataset, print the minimum number of votes required to be the winner of the presidential election in a line. No output line may include any characters except the digits with which the number is written.
6 [[123][4567][89]] [[5][3][7][3][9]] [[[99][59][63][85][51]][[1539][7995][467]][[51][57][79][99][3][91][59]]] [[[37][95][31][77][15]][[43][5][5][5][85]][[71][3][51][89][29]][[57][95][5][69][31]][[99][59][65][73][31]]] [[[[9][7][3]][[3][5][7]][[7][9][5]]][[[9][9][3]][[5][9][9]][[7][7][3]]][[[5][9][7]][[3][9][3]][[9][5][5]]]] [[8231][3721][203][3271][8843]]
107 7 175 95 21 3599