#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<int, P> E;
#define MOD (1000000007ll)
#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

int p[11111],v[11111][3333];
string s;
stack<int> st;

int solve(int i){
	int j,n=0,ret=0;
	for(j=i+1; j<p[i]; ++j){
		if(s[j]=='['){
			v[i][n] = solve(j);
			++n;
			j = p[j];
		}else{
			while(j<p[i]){
				ret *= 10;
				ret += (s[j]-'0');
				++j;
			}
			return (ret+1)/2;
		}
	}
	sort(v[i],v[i]+n);
	for(j=0; j<(n+1)/2; ++j){
		ret += v[i][j];
	}
	return ret;
}

int main(void){
	int k,n,i,j;
	cin >> k;
	while(k--){
		cin >> s;
		n = s.l_ength();
		for(i=0; i<n; ++i){
			if(s[i] == '['){
				st.push(i);
			}else if(s[i] == ']'){
				j = st.top();
				st.pop();
				p[j] = i;
			}
		}
		cout << solve(0) << endl;
	}
	return 0;
}

