#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    { int n, m,i,j,sum=0,l=0,r=0,b=0;
      string s;
      cin>>s;
        for(i=0;i<s.size();i++)
          {
              if(s[i]=='[')
              {
                  r+=1;
              }
                  if(s[i]==']'&&r>0)
                  {
                     r--;sum++;
                  }
              if(s[i]=='(')
                 b+=1;
                  if(s[i]==')'&&b>0)
                  {
                     b--;sum++;
                  }
          }cout<<sum<<endl;}}
