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


int main(){
	int n,a,b,cnt;
	cin >> n;
	string s;
	while(n--){
		cin >> s;
		a = b = cnt = 0;
		for (int i = 0; i < s.size(); i++){
			if (s[i] == '(')a++;
			if (s[i] == ')')
			{
				a--;
				if (a >= 0) cnt++;
			}
			if (s[i] == '[')b++;
			if (s[i] == ']'){
				b--;
				if (b >= 0) cnt++;
			}
			a = max(0,a);
			b = max(0,b);
		}
		cout << cnt << endl;
	}
	
	
	
	
}