#include<bits/stdc++.h>
#include<string>


using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        int octr=0;
        int c_ctr=0;
        int ans_ctr=0;
        int ans_ctr2=0;
        
      for(int i=0;i<s.length();i++)
      {
          if(s[i]=='(')
          {
              octr++;
          }
          if(s[i]=='[')
          {
              c_ctr++;
          }
          if(s[i]==')' && octr>0)
          {
              ans_ctr++;
              octr--;
          }


          if(s[i]==']' && c_ctr>0)
          {
              ans_ctr2++;
              c_ctr--;
          }


      }
      cout<<ans_ctr+ans_ctr2<<endl;
    }
}