#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

long long solve(string str) {
  int s = 0, c = 0;
  vector<long long> v;
  rep (i, str.size()) {
    if (str[i] == '[') {
      if (c == 1) s = i;
      ++c;
    } else if (str[i] == ']') {
      --c;
      if (c == 1) v.push_back(solve(str.substr(s, i - s + 1)));
    }
  }
  if (v.empty()) {
    long long res = 0;
    for (int i = 1; i < (int)str.size() - 1; ++i) res = res * 10 + str[i] - '0';
    return res / 2 + 1;
  }
  sort(v.begin(), v.end());
  long long ans = 0;
  rep (i, v.size() / 2 + 1) ans += v[i];
  return ans;
}

int main() {
  int t;
  cin >> t;
  rep (_, t) {
    string s;
    cin >> s;
    cout << solve(s) << endl;
  }
}