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

#define   f(a,b,n)        for(ll a=b;a<n;a++)
#define   readarr(arr,n)  f(i,0,n) cin>>arr[i];
#define   ll              long long
#define   pb              push_back
#define   fast            ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define   all(x)          x.begin(), x.end()
#define   mod             1000000007  //1e9+7 ans%mod
#define   w(t)            int t; cin>>t; while(t--)
#define   N               1000000



int main()
{ //my code goes here
    w(t)
    {
        string s;
        cin>>s;
        stack<char> st1;
        stack<char> st2;
        ll count=0;
        f(i,0,s.length())
        {
            if(s[i]==')')
            {
                if(st1.empty())
                { }
                else
                {
                  st1.pop();
                  count++;
                }
            }
            if(s[i]==']')
            {
                if(st2.empty())
                {
            
                }
                else
                {
                  st2.pop();
                  count++;
                }
            }
            if(s[i]=='(')
                st1.push(s[i]);
            if(s[i]=='[')
                st2.push(s[i]);
        }
        cout<<count<<endl;
    }

}