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

#define int  long long
#define tle ios_base::sync_with_stdio(false);cin.tie(NULL);
const int hell=1e9+7;
#define pb push_back


// BHENCHOD TATTE CHEEL GYE



int32_t main() 
{
    tle
	int t=1;
	cin>>t;
	while(t--)
	{
	    string s;
	    cin>>s;
	    int ct1=0,ct2=0,c3=0,c4=0,ans=0;
	    for(int i=0;i<s.size();i++)
	    {
	        //if(ct1<0)
	        
	        if(s[i]=='(')
	        ct1++;
	        if(s[i]==')')
	        {
	            if(ct1>0)
	            {
	                ct1--;
	                ans++;
	            }
	        }
	        if(s[i]=='[')
	        ct2++;
	        if(s[i]==']')
	        {
	            if(ct2>0)
	            {
	                ct2--;
	                ans++;
	            }
	        }
	        
	        
	    }
	   
	   
	    cout<<ans<<"\n";
	    
	    
	    
	    
	    
	    
	    
	    
	}
	return 0;
}
