#include <stdio.h>
#include <stdlib.h>

int calc(int number_of_district, char *index);
int count_district(char *str, char **next);
int comp(const void *a, const void *b);
char line[10010];

int main(void){
	int n,i;
	int number_of_children;
	char *end;
	scanf("%d%*c",&n);
	for(;n>0;n--){
		fgets(line, 10010, stdin);
		number_of_children=count_district(line, &end);
		//printf("number_of_children:%d\n",number_of_children);
		printf("%d\n",calc(number_of_children,line));
	}
	return 0;
}

int comp(const void *a, const void *b){
	return *(int *)a-*(int *)b;
}

int calc(int number_of_district, char *index){
	int minimum[number_of_district];
	int i,number_of_children;
	int sum=0;
	char *end;

	for(i=0;i<number_of_district;i++){
		number_of_children=count_district(index+1, &end);
		if(number_of_children==0){
			sscanf(index+2,"%d",&minimum[i]);
			minimum[i]=minimum[i]/2+1;
			index=end;
		}else{
			minimum[i]=calc(number_of_children,index+1);
			index=end;
		}
	}
	qsort(minimum, number_of_district, sizeof(int), comp);
	for(i=0;i<number_of_district/2+1;i++){
		sum+=minimum[i];
	}
	return sum;
}

int count_district(char *str, char **end){
	int count=0;
	int level=0;
	while(1){
		if(*str=='['){
			if(level==1) count++;
			level++;
		}
		if(*str==']'){
			level--;
			if(level==0){
				*end=str;
				return count;
			}
		}
		str++;
	}
}