#include<bits/stdc++.h>
using namespace std;
# define test int t;cin>>t;while(t--)
#define ff first
#define ss second
#define ll long long int
#define pb push_back
#define pii pair< int,int >
#define endl "\n"
#define mod 1000000007
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;

int main(){
    fast;
    test{
        string s;
    cin>>s;
    int ans=0,o1=0,o2=0;
    int n=s.length();
    for(int i=0;i<n;i++){
        if(s[i]=='(')o1++;
        else if(s[i]=='[')o2++;
        else if(s[i]==']'){
            if(o2){
                o2--;
                ans++;
            }
        }
        else{
            if(o1){
                o1--;
                ans++;
            }
        }
    }
    cout<<ans<<endl;
    }
}