#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char S[10005],dam[9];
int N,i,l,t;
 
int cmp(const void *a,const void *b){return *(int*)a-*(int*)b;}
 
int dfs(int s,int e)
{
	int i,j,c,p=0,t;
	int a[4000];
	for(j=s+1;j+1<e;)
	{
		if(S[j]=='[')//not leaf
		{
			for(i=j+1,c=1;i<=e&&c!=0;i++)
			{
				if(S[i]=='[')c++;
				else if(S[i]==']')c--;
			}
			a[p++]=dfs(j,i-1);
			j=i;
		}
		else//leaf
		{
			for(i=j;(c=sscanf(S+i,"%d",&t));i+=(sprintf(dam,"%d",t)+2))
				a[p++]=t;
			qsort(a,p,sizeof(int),cmp);
			for(i=t=0;i<p/2+1;i++)
				t+=a[i]/2+1;
					return t;
		}
	}
	qsort(a,p,sizeof(int),cmp);
	for(i=t=0;i<p/2+1;i++)
		t+=a[i];
	return t;
}
 
int main()
{
	for(scanf("%d\n",&N);N--;)
	{
		gets(S);
		for(i=0,l=strlen(S);i<l;i++)
		{
			if((S[i]=='['&&'0'<=S[i+1]&&S[i+1]<='9')||(S[i]==']'&&'0'<=S[i-1]&&S[i-1]<='9'))
				S[i]=' ';
		}
		//puts(S);
		printf("%d\n",dfs(0,l));
	}
	return 0;
}