#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

#define SIZE 5000

int Change(int n, int i) {
	return n * 10 + i;
}

int main() {
	string str;
	bool num;
	int count, n;
	int N[SIZE];
	vector<int> P;
	int s;
	int res;

	cin >> s;
	for (int i = 0;i < s;i++) {
		cin >> str;
		count = 0;
		n = 0;
		num = false;
		for (int j = 0;j < str.size();j++) {
			if (str[j] == '[') {
				P.push_back(count);
			}
			else if (str[j] == ']') {
				if (num) {
					N[count] = (n + 1) / 2;
					count++;
					num = false;
					n = 0;
				}
				else {
					sort(N + P.back(), N + count);
					res = 0;
					for (int k = 0;k < (count - P.back() + 1) / 2;k++) {
						res += N[k + P.back()];
					}
					count = P.back();
					N[count] = res;
					count++;
				}
				P.pop_back();
			}
			else {
				num = true;
				n = Change(n, str[j] - 48);
			}

		}
		
		cout << N[0] << endl;
	}

	return 0;
}