#include "bits/stdc++.h"
#define endl "\n"
#define ll long long int
#define vll vector<ll>
#define vvll vector < vll >
#define pll pair<long long, long long>
#define pb push_back
#define mod 1000000007
#define inf 1000000000000000001
#define mp(x,y) make_pair(x,y)
#define eb emplace_back
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)

using namespace std;

int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int t;
  cin >> t;
  while(t--) {
    string s;
    cin >> s;
    ll a=0,b=0, ans=0;
    for(auto i: s) {
      if(i=='(') a++;
      else if(i=='[') b++;
      else if(i==')') {
        if(a>0){
          a--;
          ans++;
        }
      } else {
        if(b>0) {
          b--;
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  
  return 0;
}