#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <set>
#include <map>
#define INF 100000
using namespace std;


int power(int n){
    if(n==0) return 1;
    return 10 * power(n-1);
}

int main(){
    int n;
    cin >> n;
    for (int p=0; p<n; p++) {
        string S;
        cin >> S;
        vector<int> data;
        for (int i=0; i<S.length(); i++) {
            if (S[i]=='[') {
                data.push_back(-1);
                continue;
            }
            if (S[i]==']') {
                data.push_back(-2);
                continue;
            }
            int n=0;
            vector<int> nn;
            while(1){
                if(S[i]=='[' || S[i]==']') break;
                int pp = (int)S[i] -(int)'0';
                nn.push_back(pp);
                n++;
                i++;
            }
            n--;
            i--;
            int num=0;
            int temp = n;
            for (int j=0; j<=temp; j++) {
                num += nn[j] * power(n);
                n--;
            }
            data.push_back(num);
        }
        int depth=0;
        while(data[depth]==-1){
            depth++;
        }
        /*for (int i=0; i<data.size(); i++) {
            cout << data[i] << " ";
        }
        cout << endl;*/
        vector<int> res;
        for (int i=0; i<data.size(); i++) {
            if (data[i+1]!=-1 and data[i+1]!=-2 and data[i+1]!=-3) {
                data[i+1] = (data[i+1]+1)/2;
                res.push_back(data[i+1]);
                data[i]= -3;
                data[i+2]= -3;
                i+=2;
            }
        }
        data[data.size()-1]=-2;
        /*for (int i=0; i<data.size(); i++) {
            cout << data[i] << " ";
        }
        cout << endl;*/
        depth--;
        //cout << depth<<endl;
        while (depth >= 1) {
            int dep=0;
            for (int i=0; i<data.size(); i++) {
                if (data[i]==-1) {
                    dep++;
                    continue;
                }
                if (data[i]==-2) {
                    dep--;
                    continue;
                }
                if (dep==depth) {
                    int c=i;
                    vector<int> foo;
                    while (data[c]!=-2 and c < data.size()) {
                        if (data[c]!=-3 and data[c]!=-1) {
                            foo.push_back(data[c]);
                        }
                        //cout <<'C' <<c<<endl;
                        c++;
                    }
                    sort(foo.begin(),foo.end());
                    /*for (int k=0;k<foo.size() ; k++) {
                        cout << foo[k]<<" ";
                    }*/
                    int fuga=0;
                    for (int k=0; k<foo.size()/2 +1; k++) {
                        if(foo.size()!=0) fuga += foo[k];
                    }
                    //data[c]=-3;
                    for (int j=i-1; j <= c; j++) {
                        data[j]=-3;
                    }
                    if(fuga!=0) data[(i+c)/2]=fuga;
                    i=c;
                    dep--;
                }
                /*cout <<"--"<<i<<"--"<<endl;
                for (int i=0; i<data.size(); i++) {
                    cout << data[i] << " ";
                }
                cout << endl;*/
            }
            //cout << depth <<endl;
            /*for (int i=0; i<data.size(); i++) {
                if(data[i]==-1) cout << '['<<" ";
                if (data[i]==-2) {
                    cout << ']'<<" ";
                }
                if (data[i]>=0) {
                    cout << data[i]<<" ";
                }
            }
            cout << endl;*/
            depth--;
        }
        vector<int> Ans;
        for (int i=0; i<data.size(); i++) {
            if(data[i]>=0) Ans.push_back(data[i]);
        }
        sort(Ans.begin(), Ans.end());
        int ans=0;
        for (int i = 0; i<Ans.size()/2 + 1; i++) {
            ans += Ans[i];
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 1352