import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;

void solve(string s) {
    int i = 0;
    int count() {
        if (s[i] == '[') {
            int[] xs;
            while (s[i] == '[') {
                i++; // skip '['
                xs ~= count();
                assert(s[i] == ']');
                i++; // skip ']'
                if (i == s.length) break;
            }
            xs.sort;
            return xs[0 .. $ / 2 + 1].reduce!"a + b";
        } else {
            string x;
            while (s[i].isDigit) {
                x ~= s[i];
                i++;
            }
            return x.to!int / 2 + 1;
        }
    }
    count.writeln;
}

void main() {
    int n; readf("%d\n", &n);
    foreach (_; 0 .. n) {
        solve(readln.chomp);
    }
}