// Aizu 1188: Hierarchical Democracy
// 2017.9.18 bal4u@uu

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char buf[10003];

int cmp(int *a, int *b) { return *a - *b; }

char *pair(char *p)
{
	int n = 0;
	while (1) {
		if (*p == '[') n++;
		else if (*p == ']') { n--; if (n < 0) break; }
		p++;
	}
	return p;
}

char *calc(char *p, int *v, int *half)
{
	int n, i, s, h;
	char *pp;
	int *t;

	pp = p, n = 0; while (*p == '[') p++, p = pair(p), p++, n++;
	if (n == 0) {
		s = 0; while (isdigit(*p)) s = s * 10 + *p++ - '0';
		*v = s, *half = 1; return p;
	}
	t = calloc(n, sizeof(int));
	p = pp; for (i = 0; i < n; i++) p++, p = calc(p, &s, &h), p++, t[i] = s;
	qsort(t, n, sizeof(int), cmp);
	for (s = 0, n >>= 1, i = 0; i <= n; i++) if (h) s += 1 + (t[i] >> 1); else s += t[i];
	free(t);
	*v = s, *half = 0;
	return p;
}

int main()
{
	int n, ans, t;
	char *p;

	scanf("%d", &n);
	while (n-- > 0) {
		scanf("%s", p = buf); p++;
		calc(p, &ans, &t);
		printf("%d\n", ans);
	}
	return 0;
}