#include <bits/stdc++.h>

using namespace std;

int main()
{
	int t, ans;
	cin>>t;
	while(t--)
	{
		ans = 0;
		string s;
		cin>>s;
		
		map <char, int> mp;
		
		for(auto c : s)
		{
			if(c == '[')
			{
				mp['['] += 1;
			}
			else if(c == '(')
			{
				mp['('] += 1;
			}
			
			else if(c == ']' && mp['['] > 0)
			{
				mp['[']--;
				ans++;
			}
			else if(c == ')' && mp['('] > 0)
			{
				mp['(']--;
				ans++;
			}
			
		}
		
		cout<< ans <<endl;
		
	}
}
