#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX_LEN 10000
#define MAX_NC 3333
char buff[MAX_LEN+1];
int comp(const void *a, const void *b){
    int c = *(int *)a;
    int d = *(int *)b;
    if(c<d) return -1;
    else  return 1;
}
int find_rp(int lp){
    int count=1;
    int rp=lp;
    char c;
    while(count){
        c=buff[++rp];
        if(c=='[') count++;
        else if(c==']') count--;
    }
    return rp;
}
int mc(int from, int to){
    int nv[MAX_NC];
    int nd;
    int v;
    int lp, rp;
    int i;
    if(buff[from+1]=='['){
        nd=0;
        lp=from+1;
        while(lp<to){
            rp=find_rp(lp);
            nv[nd++]=mc(lp,rp);
            lp=rp+1;
        }
        qsort(nv,nd,sizeof(int),comp);
        v=0;
        for(i=0; i<(nd+1)/2; i++) v+=nv[i];
        return v;
    }
    else{
        sscanf(&buff[from+1],"%d",&v);
        return (v+1)/2;
    }
}
int main(void){
    int n,i,ans;
    scanf("%d",&n);
    for(i=0; i<n; i++){
        scanf("%s",buff);
        ans=mc(0,strlen(buff)-1);
        printf("%d\n",ans);
    }
    return 0;
}

