#include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  int n;
  cin >> n;
  
  while (n--) {
    string s;
    cin >> s;
    
    vector<vector<ll>> x(5005);
    int it = 0;
    ll keep = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s.at(i) == '[') it++;
      else if (s.at(i) == ']') {
        if (keep > 0) {
          x[it].emplace_back(keep / 2 + 1);
          keep = 0;
        }
        else {
          sort(x[it + 1].begin(), x[it + 1].end());
          ll keep2 = 0;
          for (int j = 0; j < x[it + 1].size() / 2 + 1; j++) {
            keep2 += x[it + 1].at(j);
          }
          x[it].emplace_back(keep2);
          x[it + 1].clear();
        }
        it--;
      }
      else {
        keep *= 10;
        keep += (ll) s.at(i) - '0';
      }
    }
    
    cout << x[1].at(0) << '\n';
  }
}
