#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846

using namespace std;

//typedef
//-------------------------#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s)
{
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}
template <class T>
inline string toString(T x)
{
    ostringstream sout;
    sout << x;
    return sout.str();
}
inline int readInt()
{
    int x;
    scanf("%d", &x);
    return x;
}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SQ(a) ((a) * (a))
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
//#define MOD 1000000007

#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const double EPS = 1E-8;

#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
const int INF = 2147483647;
class UnionFind
{
public:
    vector<int> par;
    vector<int> siz;

    UnionFind(int sz_) : par(sz_), siz(sz_, 1)
    {
        for (ll i = 0; i < sz_; ++i)
            par[i] = i;
    }
    void init(int sz_)
    {
        par.resize(sz_);
        siz.assign(sz_, 1LL);
        for (ll i = 0; i < sz_; ++i)
            par[i] = i;
    }

    int root(int x)
    {
        while (par[x] != x)
        {
            x = par[x] = par[par[x]];
        }
        return x;
    }

    bool merge(int x, int y)
    {
        x = root(x);
        y = root(y);
        if (x == y)
            return false;
        if (siz[x] < siz[y])
            swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(int x, int y)
    {
        return root(x) == root(y);
    }

    int size(int x)
    {
        return siz[root(x)];
    }
};

int N;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int t;
    cin >> t;

    while (t--)
    {
        string s;
        cin >> s;
        map<int, vector<int>> mp;
        vector<pair<int, int>> vp;
        int depth = 0;
        int num = 0;
        for (int i = 0; i < s.size(); i++)
        {
            if (s[i] == '[')
            {

                depth++;
                vp.push_back(make_pair(0, depth));
            }
            else if (s[i] == ']')
            {
                vp.push_back(make_pair(num, depth));
                num = 0;

                depth--;
                vp.push_back(make_pair(0, depth + 1));
            }
            else

            {
                num = num * 10 + s[i] - '0';
            }
        }

        int turn = 0;
        while (1)
        {
            if (vp.size() <= 2)
                break;
            vector<pair<int, int>> tmp;
            for (int i = 0; i < vp.size(); i++)
            {
                int l = i, r = i;
                vector<int> v;
                while (r < vp.size() && vp[i].second == vp[r].second)
                {
                    if (vp[r].first != 0)
                        v.push_back(vp[r].first);
                    r++;
                }

                sort(all(v));

                int t = 0;
                for (int j = 0; j < (v.size() + 1) / 2; j++)
                {
                    if (turn == 0)
                    {
                        t += (v[j] + 1) / 2;
                    }
                    else
                    {
                        t += v[j];
                    }
                }
                if (t == 0)
                {
                    if (vp[i].second - 1 >= 0)
                    {
                        tmp.push_back(make_pair(t, vp[i].second));
                    }
                }
                else
                    tmp.push_back(make_pair(t, vp[i].second - 1));

                i = r - 1;
            }

            vp = tmp;
            turn++;
        }
        for (int i = 0; i < vp.size(); i++)
        {
            cout << vp[i].first << endl;
        }
    }

    return 0;
}



Measured memory_bytes: 3340