#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

struct Info{
	Info(){
		number = 0;
		open = close = false;
	}
	Info(bool isOpen,bool isClose,int arg_number){
		open = isOpen;
		close = isClose;
		number = arg_number;
	}
	int number;
	bool open,close;
};

int main(){

	int N,table[10000],index,tmp,loop,calc,ans;
	char buf[10001];

	scanf("%d",&N);

	queue<Info> Q,P;
	Info pre_info,tmp_info;

	for(int outer = 0; outer < N; outer++){

		scanf("%s",buf);

		tmp = 0;
		for(int i = 0; buf[i] != '\0'; i++){
			if(buf[i] == '['){
				if(buf[i+1] >= '0' && buf[i+1] <= '9'){
					//Do nothing
				}else{
					Q.push(Info(true,false,0));
				}
			}else if(buf[i] == ']'){
				if(buf[i-1] >= '0' && buf[i-1] <='9'){
					Q.push(Info(false,false,(tmp/2)+1));
					tmp = 0;
				}else{
					Q.push(Info(false,true,0));
				}
			}else{
				tmp = 10*tmp + (buf[i] - '0');
			}
		}

		loop = 0;
		while(true){

			if(loop%2 == 0){
				if(Q.front().open == false){
					ans = Q.front().number;
					Q.pop();
					break;
				}
				while(!Q.empty()){

					tmp_info = Q.front();
					Q.pop();

					if(tmp_info.open == true){
						if(Q.front().close == false && Q.front().open == false){
							//Do nothing
						}else{
							P.push(tmp_info);
						}
						pre_info = tmp_info;
					}else if(tmp_info.close == true){
						if(pre_info.close == false && pre_info.open == false){
							//Do nothing
						}else{
							P.push(tmp_info);
						}
						pre_info = tmp_info;
					}else{
						pre_info = tmp_info;

						index = 0;
						table[index++] = tmp_info.number;

						while(Q.empty() == false && Q.front().open == false && Q.front().close == false){
							table[index++] = Q.front().number;
							Q.pop();
						}

						for(int i = 1; i <= index-1; i++){
							for(int k = index-1; k >= i; k--){
								if(table[k] < table[k-1])swap(table[k],table[k-1]);
							}
						}

						calc = 0;

						for(int i = 0; i <= index/2; i++){
							calc += table[i];
						}

						P.push(Info(false,false,calc));
					}
				}

			}else{
				if(P.front().open == false){
					ans = P.front().number;
					P.pop();
					break;
				}
				while(!P.empty()){

					tmp_info = P.front();
					P.pop();

					if(tmp_info.open == true){
						if(P.front().close == false && P.front().open == false){
							//Do nothing
						}else{
							Q.push(tmp_info);
						}
						pre_info = tmp_info;
					}else if(tmp_info.close == true){
						if(pre_info.close == false && pre_info.open == false){
							//Do nothing
						}else{
							Q.push(tmp_info);
						}
						pre_info = tmp_info;
					}else{
						pre_info = tmp_info;

						index = 0;
						table[index++] = tmp_info.number;

						while(P.empty() == false && P.front().open == false && P.front().close == false){
							table[index++] = P.front().number;
							P.pop();
						}

						for(int i = 1; i <= index-1; i++){
							for(int k = index-1; k >= i; k--){
								if(table[k] < table[k-1])swap(table[k],table[k-1]);
							}
						}

						calc = 0;

						for(int i = 0; i <= index/2; i++){
							calc += table[i];
						}

						Q.push(Info(false,false,calc));

					}
				}
			}
			loop++;
		}

		printf("%d\n",ans);

	}

    return 0;
}

Measured memory_bytes: 2844