#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define mod 1000000007
#define vll vector<ll>
#define pll vector<pair<ll,ll>>
#define pi 3.141592653589793238

ll ab(ll x){
    if(x>0)
        return x;
    else return 0;
}


int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll t;
	cin>>t;
	//t=1;
	start:
	while(t--){

            string s;
    cin>>s;
    stack<char> c,d;
    ll ans=0;
    for(ll i=0;i<s.size();i++){
        if(c.empty())
            c.push(s[i]);
        else if(c.top()=='(' && s[i]==')')
        {
            c.pop();
            ans++;
        }
       /* else if(c.top()=='[' && s[i]==']'){
            c.pop();
            ans++;
        }*/
        else if(s[i]!=']' && s[i]!='[') c.push(s[i]);
    }
    for(ll i=0;i<s.size();i++){
        if(d.empty())
            d.push(s[i]);
      /*  else if(d.top()=='(' && s[i]==')')
        {
            d.pop();
            ans++;
        }*/
        else if(d.top()=='[' && s[i]==']'){
            d.pop();
            ans++;
        }
        else if(s[i]!=')' && s[i]!='(') d.push(s[i]);
    }
    cout<<ans<<endl;



	}
	return 0;
}
