#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = (int)(2e5) + 7;
int _,ans,res,a,b;
char s[N];
int main() {

	
	for (scanf("%d", &_); _; --_) {
		res = 0, a = 0, b = 0;
		scanf("%s", s );getchar();
		int length = (int)strlen(s);
		for (int i = 0; i < length; i++) {
			if (s[i] == '(')
				a++; 
			 if (s[i] == ')' && a != 0) { 
				a--;
				res++;
			}
			if (s[i] == '[')b++;
			 if (s[i] == ']' && b != 0) {
				b--;
				res++; 
			}
			
		}printf("%d\n", res);
	}
		
	return 0;
}