#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
int main(){
    int t;cin>>t;
    while(t--){
        int n;
        string s;cin>>s;
        n=s.size();
        int c=0;
        map<char,int> m;
        for(int i=0;i<n;i++){
            if(s[i]=='('||s[i]=='[')
                m[s[i]]++;
            else if(s[i]==')'&&m['('])
                c++,m['(']--;
            else if(s[i]==']'&&m['['])
                c++,m['[']--;
        }
        cout<<c<<"\n";
    }
}
